问题
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