There is a list of user chats, and the chat data is periodically updated. In service, data is received, and sent to the fragment. When data is received by a fragment, it is sent to the adapter, and the most interesting begins here. Data in the adapter is not updated.

That is, they are updated in ArrayList, but when I call notifyItemChanged (0) ;, the first item does not want to be updated. You must first scroll down and then up, and even then new data will be visible.

Adapter code (not all):

public class DialogAdapter extends RecyclerView.Adapter<DialogAdapter.ViewHolder> { private ArrayList items; private ArrayList<String> dialogPosition; public DialogAdapter(ArrayList items) { this.items = items; dialogPosition = new ArrayList(); Dialog dialog; for (int i = 0; i < items.size(); i++) { dialog = (Dialog) items.get(i); dialogPosition.add(String.valueOf(dialog.ownerId)); } } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ViewHolder( LayoutInflater.from(App.context).inflate(R.layout.dialog_item, parent, false) ); } @Override public void onBindViewHolder(ViewHolder holder, int position) { /* Code */ } @Override public int getItemCount() { return items.size(); } class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { View itemView; public ViewHolder(View itemView) { super(itemView); this.itemView = itemView; } @Override public void onClick(View v) { App.openChat((Dialog) items.get(getPosition()); } } public void setMsg(String body, int chat) { int position = dialogPosition.indexOf(String.valueOf(chat)); if (position != -1) { notifyItemMoved(position, 0); Dialog dialog = (Dialog) items.get(position); dialog.body = body; items.remove(dialog); dialogPosition.remove(String.valueOf(chat)); items.add(0, dialog); dialogPosition.add(0, String.valueOf(chat)); notifyItemChanged(0); } } 

}

  • You have data deleting, then inserting, and not changing content by position. Apparently notifyItemChanged() is not perceived "on your account." If you wish, try not to move the data, but simply make changes to the position, if the result is the same, then you need to decide something, otherwise use other methods of notification of changes in the data. - pavlofff
  • I think the problem may be onBindViewHolder - georgehardcore
  • @georgehardcore onBindViewHolder is not called at all - antop95
  • @pavlofff tried, does not help. What other methods of notification about changes? - antop95
  • And why, for example, items ArrayList , but not ArrayList<Dialog> , since nothing helps - georgehardcore

1 answer 1

RecyclerView works very strangely with data and its changes. It does not work, because the object has not changed, you deleted and added another object. You should notify your actions thoroughly. notifyItemRemoved(0) in combination with notifyItemInserted(0) should work.

  • Perhaps notifyDataSetChanged() more suitable, since the methods you specify include a rather noticeable animation of the disappearance and appearance of the item, that is, the list will noticeably move back and forth. - pavlofff
  • RecyclerView needs to be as accurate as possible. Often, notifyDataSetChanged does not work. You can google it. - Kaerdan
  • Your method doesn't work - antop95