Reputation: 21
For example, I have a model class name movies and I am fetching the list of movies from an api
and I got List<movies> list
then I pass this list in recycler view and triggered notifydatasetchanged()
.
Thenmy question is to bind the data in the XML Should I have to expose my model class for databinding. Is this a correct mvvm pattern?
How can I achieve it by binding my XML to ViewModel
class?
Upvotes: 0
Views: 438
Reputation: 87
You can use data binging for your recycler view. You can add:
<data><variable name="movie" type="com.example.myproject.model.Movie"/>
</data>
tag to your recycler view row item XML layout.
Then inside the View Holder
class you can implement binding method like this
public void binding(Movie movie){
movieRowBinding.setMovie(movie);
movieRowBinding.executePendingBindings();
}
Upvotes: 1