添加评论时更新 RecyclerView

Update RecyclerView When Adding Comment

我正在制作 CommentFragment。我有一个 RecyclerView 来列出评论和一个 Edittext 来写评论。但是,当我添加评论时,它会发送到服务器,但 RecyclerView 不会更新。我使用 notifydatasetchanged 来更新。代码:

 private void getComments(){

    Call<List<CommentsModel>> call=restApiClass.getComments(post_id);
    call.enqueue(new Callback<List<CommentsModel>>() {
        @Override
        public void onResponse(Call<List<CommentsModel>> call, Response<List<CommentsModel>> response) {

            if(response.isSuccessful()){

                list=response.body();
                if(commentsAdapter==null) {
                    commentsAdapter = new CommentsAdapter(list, getContext());
                }
                else{
                    commentsAdapter.notifyDataSetChanged();
                }
                recyclerView.setAdapter(commentsAdapter);
            }

        }
        @Override
        public void onFailure(Call<List<CommentsModel>> call, Throwable t) {
        }
    });
}

我在点击发送CommentTextView时调用了这个方法:

  sendCommentTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

              //............send comment codes.........
               getComments();

        }
    });

检查此正确答案:notifyDataSetChanged example

我认为您应该按照那里的答案使适配器使用 notifyDataSetChanged() 按预期工作。很快:您必须更新适配器内的列表,然后通知它列表已更新,因此它将在 ui(recyclerview)上再次呈现。

PS: 在 notifyDataSetChanged 函数之后,如果已经分配,​​则无需在 recyclerview 上再次设置适配器。

这里您在 Retrofit 带回新数据时更新了 list,但实际上您并没有将这些数据提供给您的适配器;你刚刚调用了 commentsAdapter.notifyDataSetChanged();,但你需要先在你的适配器中添加一个像 updateList(List<CommentsModel> list) 的方法,并在使用 commentsAdapter.notifyDataSetChanged();

之前用 updateList(list); 调用它

因此,将 updateList(List<CommentsModel> list) 添加到您的适配器中以在内部更新其列表,如下所示:

class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.ViewHolder> {
    ...

    List<CommentsModel> mList;

    updateList(List<CommentsModel> list) {
        this.mList.clear();
        this.mList.addAll(list);
    }

}

然后您的代码中的更改可能类似于:

call.enqueue(new Callback<List<CommentsModel>>() {
    @Override
    public void onResponse(Call<List<CommentsModel>> call, Response<List<CommentsModel>> response) {

        if(response.isSuccessful()){

            list=response.body();
            if(commentsAdapter==null) {
                commentsAdapter = new CommentsAdapter(list, getContext());
            }
            else{
                commentsAdapter.updateList(list);
                commentsAdapter.notifyDataSetChanged();
            }
            recyclerView.setAdapter(commentsAdapter);
        }

    }
    @Override
    public void onFailure(Call<List<CommentsModel>> call, Throwable t) {
    }
});

Update

Yes it's working but recylerview return to first position and list reload. But I want to load only last comment. Other comments must stay same. So how can I do that ?

要仅更新列表中的最后一条评论,然后将一个新方法添加到您的适配器中,该方法只接受该评论,并将其添加到列表中,如:

class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.ViewHolder> {
    ...

    List<CommentsModel> mList;

    addComment(CommentsModel comment) {
        this.mList.add(comment);
        notifyItemInserted(mList.size()-1);
    }

}

然后当数据通过Retrofit返回时:

call.enqueue(new Callback<List<CommentsModel>>() {
    @Override
    public void onResponse(Call<List<CommentsModel>> call, Response<List<CommentsModel>> response) {

        if(response.isSuccessful()){

            list = response.body();
            if(commentsAdapter == null) {
                commentsAdapter = new CommentsAdapter(list, getContext());
            }
            else{
                // updating the adapter list with the last comment 
                commentsAdapter.addComment((CommentsModel) list.get(list.size()-1));

            }
            recyclerView.setAdapter(commentsAdapter);
        }

    }
    @Override
    public void onFailure(Call<List<CommentsModel>> call, Throwable t) {
    }
});