https://kotlinlang.org logo
Title
r

rezenebe

11/23/2018, 3:53 AM
Is it bad practice to pass a fragment in as an adapter's parameter so you can access it's methods?
e

edwardwongtl

11/23/2018, 4:53 AM
Yes, but what exactly do you want from a fragment inside a adapter?
r

rezenebe

11/23/2018, 2:48 PM
I want my recyclerview to access the same viewmodel as my fragment
d

dewildte

11/23/2018, 3:33 PM
That sounds highly suspicious
Do you have a particular reason why?
r

rezenebe

11/23/2018, 3:42 PM
My viewholder class is handling a lot of logic for the view items, 400 lines of code worth. I'm trying to think of a way to have some seperation and clean up this class. I'm using mvvm and have a viewmodel for my fragment already. I figured one solution was to move some of the functionality to that same viewmodel
t

tank777

11/24/2018, 2:12 AM
You can use the Rx kotlin or Rx java to do that. No need to pass the fragment.
d

dewildte

11/24/2018, 4:12 AM
View related logic is fine to have in the holder but you should try to extract all the other logic into the
viewModel
.
You could use a custom
onClickListener
to pass the relevant data class in the
ViewHolder
to the
Fragment
. The
Fragment
in turn makes a request of the
ViewModel
to do what ever it's got to do with said data class and then update the data set for rebinding.
None of this needs Rx anything IMHO.
Using Rx just to extract logic out of a
ViewHolder
is like using a transport truck to hammer a nail.
1