update RecyclerView with Android LiveData

前端 未结 4 999
感动是毒
感动是毒 2021-01-31 16:13

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

相关标签:
4条回答
  • 2021-01-31 16:46

    Like @Lyla said, you should observe the whole list as LiveData in Fragment or Activity, when receive changes, you should set the whole list to the adapter by DiffUtil.

    Fake code:

    PostViewModel {
        LiveData<List<Post>> posts;  // posts comes from DAO or Webservice
    }
    
    MyFragment extends LifecycleFragment {
        PostAdapter postAdapter;
    
        ...
    
        void onActivityCreated() {
            ...
            postViewModel.posts.observer(this, (postList) -> {
                postAdapter.setPosts(postList);
            }
        }       
    }
    
    PostAdapter {
        void setPosts(List<Post> postList) {
            DiffUtil.DiffResult result = DiffUtil.calculateDiff(new DiffUtil.Callback() {...}
            ...
        }
    }
    
    0 讨论(0)
  • 2021-01-31 16:47

    Using DiffUtil might help with updating one row in a huge list. You can then have LiveData wrap the list of comments instead of a single comment or attribute of a comment.

    Here's an example of using DiffUtil within a RecyclerView adapter and the list LiveData observation code in the fragment.

    0 讨论(0)
  • 2021-01-31 16:48

    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> post = new MutableLiveData<>();
    
        public PostViewHolder(View itemView) {
            super(itemView);
    
            LiveData<String> name = Transformations.switchMap(post, new Function<Post, LiveData<String>>() {
                @Override
                public LiveData<String> apply(Post input) {
                    return input.getLiveName();
                }
            });
    
            name.observeForever(new Observer<String>() {
                @Override
                public void onChanged(@Nullable String name) {
                    // use name
                }
            });
        }
    
        public void bind(Post post) {
            post.setValue(post);
        }
    }
    
    0 讨论(0)
  • 2021-01-31 16:58

    add context to your adapterClass construstor : AdpaterClass(context: Context)

    then smart cast the context to AppCompactActivity

    livedata.observe(context as AppCompatActivity, Observer {it ->
    
            //perform action on it(livedata.value)
        })
    

    when calling the adapter from anywhere activity, fragment pass the context into the adpater

    0 讨论(0)
提交回复
热议问题