andreasmichaelides / SnazzyRecycling

A simpler way of using the Android RecyclerView, to help you speed up development
MIT License
0 stars 0 forks source link

Having issue with click listener handled by PublishSubject #1

Open alicewang05 opened 4 years ago

alicewang05 commented 4 years ago

Hope you are well.

After use this way to handle recyclerview, I came to an issue with the click listener in the recyclerview. I find a way to fix it, just want to see if you have better way.

The app has BottomNavigationView, got 5 tabs. FragmentA has recyclerview which uses this pattern and FragmentA has androidx.lifecycle.viewMode that handle the itemClicked, and FragmentA could be shown in tab 1 and tab 2 at the same time. when trigger a click on the recyclerview item of FragmentA on tab2, PublishSubject.onNext() is triggered, the issue is when this happens, both FragmentA on tab 1 and tab2 handle this click event. FragmentA on tab1 should not do that.

The way I fix this is create a boolean isViewDestoryed in androidx.lifecycle.viewModel, and update isViewDestoryed in onCreateView() and onDestroyView() in Fragment. so the viewModel only handles the itemClicked when isViewDestoryed is false.

Do you have better way to handle the click listener, maybe not use PublishSubject to handle the click listener, because have to remember to create a boolean in viewModel if the fragment has recyclerview.

Looking forward to hearing from you. let me know if anything not make sense.

andreasmichaelides commented 4 years ago

Hi Alice, I'm great, hope you're doing well too and you're enjoying the project you're working on. I've just seen your message. I'll have a look when I have some free time, and get back to you. Things are pretty busy here for me at the moment and might take some time to have a look. Thanks for the feedback 😃