What is a good implementation when updating items that change many times in RecyclerView (swap) and immediate user feedback?

Goal (s) :

  • 1: Easy update for dynamic elements.

    • Example:
      • I have a List<T>returned API, I use this list in my own RecyclerView.Adapter. The user checks to see if the new API is updated, containing some new elements and some updated old elements. Now the older list should remove duplicate items.
    • Note. Suppose all elements have an attribute updatedthat can change if the user interacts with it.
  • 2: Immediate feedback from the user (this may be due to goal number 1).

    • Example:
      • To insert a new element in RecyclerView.Adapter, it must be created in the API first. An implementation creates an object in RecyclerView.Adapterand in the API at the same time. When a new object returns from the API, the immediate object that was previously entered immediately into RecyclerView.Adapter"syncs" with the response of the API. Thus, the user sees immediate feedback.

Code example:

I have nothing in mind for goal number 1, but for goal 2 I was thinking something like this, maybe inside mine ViewHolder? (I heard that the update / sync models in "The Occupants" are not good practice in general because viewers are reworking):

// JAVA 7
private void createNewObjectToBeInsertedIntoRecyclerView(String data) {

    // Pass callback to API and at the same time insert object into adapter
    mAdapter.addNewObject(data);
    mPresenter.createObject(new SyncRequestCallback() {
        @Override
        public void onSuccessFromAPI(ModelObject model) {
            mAdapter.updateObject(model);
        }
    });
}

// JAVA 8
private void createNewObjectToBeInsertedIntoRecyclerView(String data) {

    // Pass callback to API and at the same time insert object into adapter
    mAdapter.addNewObject(data);
    mPresenter.createObject((sync) -> { mAdapter.updateObject(model); });
}

Itโ€™s just on my head, and itโ€™s definitely a mistake.

How can I achieve this? :

Looking for a reliable solution here, but something that is not related to content providers (if possible).
+4
1

ViewHolder, , API, .

, Adapter

  • List<T> API, (oldList = newList) mAdapter.notifyDataSetChanged()
  • , 1), Adapter. , Adapter ( , ), mAdapter.notifyItemInserted(position) , , Adapter, mAdapter.notifyItemChanged(position)
-1

Source: https://habr.com/ru/post/1628138/


All Articles