RecyclerView listen to item added

假如想象 提交于 2019-12-07 19:44:29

问题


I have a RecyclerView and I use the following line to add new items:

recyclerAdapter.notifyItemInserted(newItemIndex);

Is there a listener I can use to know when the RecyclerView has finished adding the item?


回答1:


I would try creating a subclass of the DefaultItemAnimator overriding the onAddFinished method.

public class MyDefaultItemAnimator extends DefaultItemAnimator {

  @Override public void onAddFinished(RecyclerView.ViewHolder item) {
    super.onAddFinished(item);

    String text = "Add element: " + item.getPosition();
    Toast.makeText(MyActivity.this, text, Toast.LENGTH_SHORT).show();
  }
}

And then:

mRecyclerView.setItemAnimator(new MyDefaultItemAnimator());

In this way you should be notified of when the add animation finished.



来源:https://stackoverflow.com/questions/27633654/recyclerview-listen-to-item-added

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!