I have a Fragment that contains:
- A ViewPager2 which holds several Fragments for left-right swiping
- These Fragments only contains a RecyclerView which displays a list of items in it
- A custom view (LinearLayout)
What I want to achieve is when user click on the custom view, it will update the data in the RecyclerView.
In my case, when a user unselects an item in the custom view, it will also unselect what is inside the RecyclerView items.
I'm not sure how to do this with a listener that goes back down to the RecyclerView's Adapter level. Anyone have any idea on how to achieve this?
CodePudding user response:
you can use BroadcastReceiver for this. In cases you want to trigger, when you broadcast, the relevant code will be triggered on the Fragment you want to capture the broadcast.
You can also use the EventBus library. Both methods will work, but the EventBus manufacturer claims that they are more performant than BroadcastReceiver.
CodePudding user response:
You can communicate using:
SharedViewModels: Both Fragments use same SharedViewModel, 1 will update the Observable(LiveData/Flow) in viewModel and another fragment will observe the changes for that Observable(LiveData/Flow)
Flows/LiveData: exposed from any Repository or UseCase layer Similar to SharedViewModels, but your Observable(LiveData/Flow) will be in either Repository or UseCase and your both viewModels of both fragments will be injecting same Repository/UseCase.
RoomDB: Store that data in RoomDB and return Flow/LiveData from Room DAO. And observe this flow in your fragment. Whenever the DB changes, your fragment will be notified with updated data.