There are many examples how to push new list to adapter on LiveData change.
I\'m trying to update one row (e.g number of comments for post) in the huge list. It would be
Use Transformations.switchMap()
to swap the underlying Post
object. Then there is no need to remove and re-add observers when the cell is recycled.
@Override
public void onBindViewHolder(PostViewHolder vh, int position) {
Post post = getPost(position);
vh.bind(post);
}
Then in your ViewHolder class
public class PostViewHolder extends RecyclerView.ViewHolder {
private final MutableLiveData post = new MutableLiveData<>();
public PostViewHolder(View itemView) {
super(itemView);
LiveData name = Transformations.switchMap(post, new Function>() {
@Override
public LiveData apply(Post input) {
return input.getLiveName();
}
});
name.observeForever(new Observer() {
@Override
public void onChanged(@Nullable String name) {
// use name
}
});
}
public void bind(Post post) {
post.setValue(post);
}
}