5

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.

BlackM
  • 3,927
  • 8
  • 39
  • 69
Simon
  • 19,658
  • 27
  • 149
  • 217

1 Answers1

12

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:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    public void onScrollStateChanged(RecyclerView rv, int state) {
        if (state == RecyclerView.SCROLL_STATE_IDLE) {
            PostList.add(0, post);
            mAdapter.notifyItemInserted(0);
            rv.removeOnScrollListener(this);
        }
    }
});
recyclerView.smoothScrollToPosition(0);

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.

yigit
  • 37,683
  • 13
  • 72
  • 58
  • Hi, thanks for your response - i don't seem to be able to find addOnScrollListener or removeOnScrollListener method in the recyclerview class even though its listed as a method in the docs https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#removeOnScrollListener(android.support.v7.widget.RecyclerView.OnScrollListener). I'm stumped, I'm using com.android.support:recyclerview-v7:22.0.+ – Simon Jun 21 '15 at 09:58
  • 1
    Use 22.2. Before, there was setOnScrollListener. – yigit Jun 21 '15 at 10:02
  • 1
    Thanks - the code works if I add one more line to the method: mRecyclerView.smoothScrollToPosition(0); – Simon Jun 21 '15 at 18:15
  • Oh, you want the newly added item to be visible. You can also just call scrollToPosition(0) when you add the item. No need for smooth scroll – yigit Jun 21 '15 at 18:17
  • @yigit I have a RecyclerView list and try to call scrollToPosition(0) with no luck. Can you review http://stackoverflow.com/questions/41686194/android-how-do-i-return-to-to-top-of-recyclerview-after-adding-item?noredirect=1#comment70569832_41686194 and see if you have any thoughts on how to fix? – AJW Jan 16 '17 at 23:50