'RecyclerView scroll to desired position without animation
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
Solution 1:[1]
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);
}
});
Solution 2:[2]
Add this line in your layout.xml
android:overScrollMode="never"
EDIT:
Another way to scroll recycleview
recyclerView.smoothScrollToPosition(position);
Solution 3:[3]
You can scroll without animation with:
recyclerView.scrollToPosition(position)
Solution 4:[4]
This can be re-produced easily when you calling notifyItemInserted() with scrollToPosition() at same time.
Animation is caused by DefaultItemAnimator in RecyclerView default item animator class.
I found Youtube video that re-produced problem by other person.
And I has solution too:
- First, scroll to last position with
scrollToPosition(). - Second, wait a little bit to let layout manager to finish scrolling.
- Third, add item into your adapter data set.
- Fourth, calling
notifyItemInserted()andscrollToPosition()now.
I have code example but written in c# , but it does same to
kotlin with lambda expression. If you need accessing RecyclerView inside Adapter, you might want to look into Adapter.onAttachedToRecyclerView().
if (Adapter.ListData.Count > 2) Adapter.RecyclerView.ScrollToPosition(Adapter.ListData.Count - 1);
Adapter.RecyclerView.PostDelayed(() =>
{
Adapter.ListData.Add(new ItemSimplyfiedAdapter.Standard
{
//Some new item inserting into data set.
});
Adapter.NotifyItemInserted(Adapter.ListData.Count - 1);
Adapter.RecyclerView?.ScrollToPosition(Adapter.ListData.Count - 1);
}, 100);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ZeGerm4n |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | Pang |
