I'm trying to create a Recyclerview that will scroll to the top first and then animate the addition of an item onto the recyclerview.
This is the code I have so far:
while (!mLayoutManager.isSmoothScrolling()) {
mRecyclerView.smoothScrollToPosition(0);
}
PostList.add(0, post);
mAdapter.notifyItemInserted(0);
mAdapter.notifyItemRangeChanged(1, PostList.size());
This does scrolls to the top but the addition of the item is not animated (although it is added to the list).
I think it is because the addition animation occurs at the same time as the smoothScrollToPosition
animation and therefore when it has reached the top, the addition animation is already finished so we cannot see it.
I can use a Handler.postDelayed
to give my scroll animation some time to finish, but that is not preferable as I don't know the time that the smoothScrollToPosition
animation will finish.
I guess you are hoping that when do while is finished, scroll will be complete. It is not how it works, scrolling happens in animation frame and if you were to put a while loop waiting for it to finish, your app will freeze because you'll be blocking main thread.
Instead, you can do something like this:
Didn't test the code but the basic idea is to add a scroll listener to get notified when smooth scrolling stops and then add the item.