What's the best architecture for handling events i...
# android
k
What's the best architecture for handling events in a recyclerview adapter that need to make network calls? So each cell can be clicked on to launch other actions, which may include network calls. I would assume you would not make network calls directly from the adapter. But should the adapter hold a reference to the fragment's ViewModel, and thereby be able to make network calls directly via the view model, or should the adapter just pass the call to the fragment and the fragment calls the viewModel? So it would be adapter -> ViewModel -> network call, or adapter -> fragment -> ViewModel -> network call. The adapter NOT holding a reference to the ViewModel feels right to me, but I'm wondering what's best, or if it matters.
t
You could initialize the adapter along with a click listener for items:
Copy code
Adapter(onItemClick: (Item) -> Unit)
When you set up the
RecyclerView
+
Adapter
in the
Fragment
etc, then you can do something like:
Copy code
Adapter { item -> viewModel.networkCall(...) }
k
thank you!
👍 1