本文介绍了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.
这篇关于RecyclerView收听添加的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!