Is it bad practice to pass a fragment in as an ada...
# codereview
r
Is it bad practice to pass a fragment in as an adapter's parameter so you can access it's methods?
e
Yes, but what exactly do you want from a fragment inside a adapter?
r
I want my recyclerview to access the same viewmodel as my fragment
d
That sounds highly suspicious
Do you have a particular reason why?
r
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
You can use the Rx kotlin or Rx java to do that. No need to pass the fragment.
d
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