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); } } }
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. - pavlofffonBindViewHolder- georgehardcoreArrayList, but notArrayList<Dialog>, since nothing helps - georgehardcore