Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView scroll to desired position without animation

Tags:

android

Previously, when I want to scroll a ListView to a desired position, I will use

listView.setSelection(row);

The ListView will be scrolled without any animation - Android List View set default position without animation

Now, I want to achieve the same effect on RecyclerView. I try to perform

((LinearLayoutManager)recyclerView.getLayoutManager()).scrollToPositionWithOffset(row, 0);

However, there is scrolling animation, which I would like to avoid.

Is there any way I can perform programmatically scrolling, without animation?

This is the animation I meant - https://youtu.be/OKsUKwBLoks

Note, I had already tried the following ways. All of them do generate animation.

  • scrollToPositionWithOffset
  • smoothScrollToPosition
  • scrollToPosition
like image 960
Cheok Yan Cheng Avatar asked Dec 06 '15 12:12

Cheok Yan Cheng


People also ask

Whats Recycler View?

RecyclerView is the ViewGroup that contains the views corresponding to your data. It's a view itself, so you add RecyclerView into your layout the way you would add any other UI element. Each individual element in the list is defined by a view holder object.

What is a Recycler View in android?

What is RecyclerView in Android? The RecyclerView is a widget that is more flexible and advanced version of GridView and ListView. It is a container for displaying large datasets which can be scrolled efficiently by maintaining limited number of views.


1 Answers

I know I'm a little late with this answer but it may help someone else. Try following (at least I could not see any animation when using it):

Kotlin:

    //-- Immediately jump to the position in RecyclerView without delay or animation.
    mRecyclerView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            mRecyclerView.scrollToPosition(position)
            mRecyclerView.viewTreeObserver.removeOnGlobalLayoutListener(this)
        }
    })

Java:

mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            mRecyclerView.scrollToPosition(position);
            mRecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
});
like image 77
ZeGerm4n Avatar answered Sep 20 '22 19:09

ZeGerm4n