添加评论时更新RecyclerView

时间:2020-04-18 21:14:59

标签: android android-recyclerview retrofit notifydatasetchanged

我正在做一个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) {
        }
    });
}

当我单击以发送sendCommentTextView时,我调用此方法:

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

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

        }
    });

2 个答案:

答案 0 :(得分:2)

检查以下正确答案:notifyDataSetChanged example

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

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

答案 1 :(得分:1)

list带回新数据时,您在这里更新了Retrofit,但实际上您并未向适配器提供此数据;您刚刚调用了commentsAdapter.notifyDataSetChanged();,但是首先需要在适配器中添加类似updateList(List<CommentsModel> list)的方法,并在使用updateList(list);之前用commentsAdapter.notifyDataSetChanged();进行调用

因此,将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) {
    }
});

更新

是的,但是recylerview返回第一个位置并列出 重新加载。但我只想加载最后的评论。其他评论必须保留 相同。那我该怎么办呢?

要仅更新列表中的最后一条注释,然后将仅包含该注释的新方法添加到适配器中,然后将其添加到列表中,例如:

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) {
    }
});