Recyclerview - smoothScrollToPosition to Top of list and then animate the addition of item

11,919

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.

Share:
11,919
Simon
Author by

Simon

I started as a really novice android developer. Like as in I just started reading a book. But I have learnt a lot now and what an experience it has been. Lately, I have already been really interested in using Zipline for Algorithmic Trading. So I started a blog on this topic, you can find it here: https://financialzipline.wordpress.com I have also developed a fully-fledged android app: https://play.google.com/store/apps/details?id=com.bekwaai&hl=en

Updated on June 04, 2022

Comments

  • Simon
    Simon about 2 years

    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.

  • Simon
    Simon about 9 years
    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 developer.android.com/reference/android/support/v7/widget/…. I'm stumped, I'm using com.android.support:recyclerview-v7:22.0.+
  • yigit
    yigit about 9 years
    Use 22.2. Before, there was setOnScrollListener.
  • Simon
    Simon about 9 years
    Thanks - the code works if I add one more line to the method: mRecyclerView.smoothScrollToPosition(0);
  • yigit
    yigit about 9 years
    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
  • AJW
    AJW over 7 years
    @yigit I have a RecyclerView list and try to call scrollToPosition(0) with no luck. Can you review stackoverflow.com/questions/41686194/… and see if you have any thoughts on how to fix?