This is the sample app for the following article: https://proandroiddev.com/flexible-recyclerview-adapter-with-mvvm-and-data-binding-74f75caef66a
-
If you want to handle it in the
ItemViewModel
, just define a function there and for the root view of theItemViewModel
in the XML add the onClick with DataBinding. An example of this can be seen at the end of the story in the Making an ItemViewModel observable section. -
If you want to handle the onClick in the
CarListViewModel
, you can do the same, just pass a lambda to theItemViewModel
in the constructor and you can call it from the XML. -
If you want to handle it in the Fragment for example showing a dialog or navigating to an other screen you would need some kind of observable LiveData, where you push events, when an onClick happens. The fragment would observe that and execute the correct action. Example here.
Example of this can be found in this commit.