我在我的聊天应用程序中使用了RecyclerView。我从API获取聊天列表。在Android中使用RecyclerView聊天应用程序
如果我发送任何消息,它不会在RecyclerView中更新。如果我回去并参加该活动,它会在RecyclerView中更新。我用adapter.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);
}
究竟为什么你要为一个新的适配器和新的布局管理器每次你得到的响应时间? – EpicPandaForce