Java android中带有RecyclerView的聊天应用程序

Java android中带有RecyclerView的聊天应用程序,java,android,android-recyclerview,Java,Android,Android Recyclerview,我正在聊天应用程序中使用RecyclerView。我从API中获取聊天列表 如果我发送任何消息,它不会在RecyclerView中更新。如果我回去参加那个活动,它会在RecyclerView中更新。我使用了适配器.notifyDataSetChanged()不起作用 call.enqueue(new Callback<LiveChatGetMsgResponse>() { @Override public void onRespons

我正在聊天应用程序中使用RecyclerView。我从API中获取聊天列表

如果我发送任何消息,它不会在RecyclerView中更新。如果我回去参加那个活动,它会在RecyclerView中更新。我使用了
适配器.notifyDataSetChanged()不起作用

 call.enqueue(new Callback<LiveChatGetMsgResponse>() {
            @Override
            public void onResponse(Call<LiveChatGetMsgResponse> call, Response<LiveChatGetMsgResponse> response) {
                if (response.isSuccessful()) {
                    LiveChatGetMsgResponse resp = response.body();
                        //Here i get the message list, i send this list to adapter class
                        msg_list = resp.getData();
                        myLinear=new LinearLayoutManager(ChatActivity.this);
                        myLinear.setReverseLayout(true);
                        recyclerChatList.setLayoutManager(myLinear);
                        adapter = new LiveChatListAdapter(getApplicationContext(), msg_list);
                        recyclerChatList.setAdapter(adapter);
                    //i use this method to scrroll down the RecyclerView
                        scrollToBottom();
                }
            }
            @Override
            public void onFailure(Call<LiveChatGetMsgResponse> call, Throwable t) {
                Log.e("LiveChatGetMsgFailure",t.getMessage());
            }
        });

private void scrollToBottom() {
        adapter.notifyDataSetChanged();
        if (adapter.getItemCount() < 1)
        recyclerChatList.getLayoutManager().smoothScrollToPosition(recyclerChatList, null, adapter.getItemCount() - 1);
    }
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
LiveChatGetMsgResponse resp=response.body();
//在这里我得到了消息列表,我将这个列表发送到适配器类
msg_list=resp.getData();
myLinear=新的LinearLayoutManager(ChatActivity.this);
myLinear.setReverseLayout(真);
recyclerChatList.setLayoutManager(myLinear);
adapter=新的LiveChatListAdapter(getApplicationContext(),msg_list);
recyclerChatList.setAdapter(适配器);
//我使用这种方法来清除回收视图
scrollToBottom();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(“LiveChatGetMsgFailure”,t.getMessage());
}
});
私有无效滚动条Tobottom(){
adapter.notifyDataSetChanged();
if(adapter.getItemCount()<1)
recyclerChatList.getLayoutManager().smoothScrollToPosition(recyclerChatList,null,adapter.getItemCount()-1);
}

您可以在适配器中使用updateData方法,如下所示

public void updateData(List<YourItem> yourItems){
     mData.clear();
     mData.addAll(yourItems);
     notifyDatasetChanged();
}

试试它,告诉我们它是否解决了问题。

您可以在适配器中使用updateData方法,如下所示

public void updateData(List<YourItem> yourItems){
     mData.clear();
     mData.addAll(yourItems);
     notifyDatasetChanged();
}

尝试一下,告诉我们它是否解决了问题。

如果您是发件人,也许您可以添加此功能:

.... adapter{
    public void addItem(CustomClass customClass){
         yourListOfObject.add(customClass);
         notifyItemInserted(yourListOfObject.size()-1);
    }
}

如果您是发件人,则可能可以添加此功能:

.... adapter{
    public void addItem(CustomClass customClass){
         yourListOfObject.add(customClass);
         notifyItemInserted(yourListOfObject.size()-1);
    }
}

为什么每次收到响应时都要设置新适配器和新布局管理器?为什么每次收到响应时都要设置新适配器和新布局管理器?在onResponse方法中:updateData(resp.getData);在onResponse方法中:updateData(resp.getData);