RecyclerView收听添加的项目

问题描述:

我有一个RecyclerView,并使用以下行添加新项目:

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

recyclerAdapter.notifyItemInserted(newItemIndex);

当RecyclerView完成添加项目时,我可以使用一个侦听器吗?

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

我将尝试创建覆盖onAddFinished方法的DefaultItemAnimator的子类.

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();
  }
}

然后:

mRecyclerView.setItemAnimator(new MyDefaultItemAnimator());

通过这种方式,应在添加动画结束时通知您.

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