I make a simple chat. Faced a problem: when sending a new message there is no data change in the Recycler View, which acts as a list of messages. This list is updated as soon as I start scrolling it or hiding the keyboard. Googled a bit, tried a couple of ways to fix it. Nothing present in the code below corrected my problem: (Actually, the code itself: (json comes to this method with a new message and I add it to the list)

@Override public void onFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { Log.i("fg", "onFrame"); JSONObject data = new JSONObject(frame.getPayloadText()); if(data.has("data")) { MyMessage myMessage = new MyMessage(data.getString("user_id"), data.getString("data"), data.getString("login"), data.getString("unix_time"), data.getString("user_id"), data.getString("avatar")); adapter.addToStart(myMessage, true); adapter.update(myMessage); } adapter.notifyDataSetChanged(); adapter.notifyItemInserted(adapter.getItemCount()); } 

Thank you for your help!

  • notifyDataSetChanged works only with collections passed to the adapter's constructor. - Flippy
  • one
    Judging by the name of adapter.addToStart(myMessage, true); You have added item to the top of the list, and to the adapter you say that at the end, it may be necessary: adapter.notifyItemInserted(0); - woesss
  • So the problem is that the list is not updated exactly? Or is it updated, but somewhere above it is necessary that it automatically slips? Add the code for the addToStart and update adapter methods. - eugeneek

1 answer 1

After adding item -a, it is also necessary that recyclerView scroll programmatically. I do it myself

 messagesRecyclerView.scrollToPosition(messages.size() - 1); 
  • Is it possible to update the list without scrolling? When a message comes from the interlocutor, the message list also scrolls, which, of course, should not be. I would be grateful for any of your ideas! - Vlad Sapozhnikov