How to use RecyclerView.scrollToPosition() to move the position to the top of current view?
Asked Answered
M

6

57

The RecyclerView.scrollToPosition() is extremely strange. for example, supposed a RecyclerView named "rv".

  1. if now item 10 is under the current RecyclerView, call rv.scrollToPosition(10), the RecyclerView will scroll item 10 to bottom.

  2. if now item 10 is in the current RecyclerView, call rv.scrollToPosition(10), there won't be any scrolling, nothing will be done.

  3. if now item 10 is on the top of the current RecyclerView, call rv.scrollToPosition(10), the RecyclerView will scroll item 10 to top.

To help understanding, look at this picture enter image description here

But what i need is that, whenever i call it, the RecyclerView will scroll the supposed position to the top of current view just like case 3. How to do that?

Memorabilia answered 25/10, 2015 at 10:47 Comment(6)
#26875561Opportunity
That's exactly what i want ! thank u so much! ummm...i think i should delete my question...Memorabilia
use linearLayoutManager.scrollToPositionWithOffset(2, 20); see Scroll RecyclerView to show selected item on topColecolectomy
Possible duplicate of Scroll RecyclerView to show selected item on topLauryn
#26875561Cookhouse
I made +1 in this question because of its presentation.it is good way to explain the problem.Wende
T
66

If I understand the question, you want to scroll to a specific position but that position is the adapter's position and not the RecyclerView's item position.

You can only achieve this through the LayoutManager.

Do something like:

rv.getLayoutManager().scrollToPosition(youPositionInTheAdapter).
Thais answered 25/10, 2015 at 12:37 Comment(5)
How do I pass the AdapterPosition to this method?Hermaphroditus
I'm certain that this is incorrect. Calling scrollToPosition on the RecyclerView just calls the same method on the LayoutManager. It even says in the javadocs "RecyclerView does not implement scrolling logic, rather forwards the call to android.support.v7.widget.RecyclerView.LayoutManager#scrollToPosition Calling scrollToPosition on the RecyclerView or the LayoutManager will have the exact same effect.Depict
@Lauryn 's answer with the StaggeredGridLayoutManager greatly worked! (See below)Mascon
Use linearLayoutManager.scrollToPositionWithOffset(position, 0);Perugia
@Castor thanks for this. A seemingly minor, yet critical distinction - scrollToPosition appears to ensure the target view is visible somewhere in the recycler (often the bottom), while scrollToPositionWithOffset 0 ensures the target view is visible at the TOP of the recycler.Adductor
L
28

Below link might solve your problem:

https://mcmap.net/q/110563/-recyclerview-how-to-smooth-scroll-to-top-of-item-on-a-certain-position

Just create a SmoothScroller with the preference SNAP_TO_START:

RecyclerView.SmoothScroller smoothScroller = new 
LinearSmoothScroller(context) {
   @Override protected int getVerticalSnapPreference() {
       return LinearSmoothScroller.SNAP_TO_START;
   }
};

Now you set the position where you want to scroll to:

smoothScroller.setTargetPosition(position);

And pass that SmoothScroller to the LayoutManager:

layoutManager.startSmoothScroll(smoothScroller);
Limicoline answered 29/4, 2017 at 14:44 Comment(6)
That's ok if you wan to do a smooth scroll, but what if you want to do an automatic scroll?Empoison
If you want to automatic scroll down to some position, in that case this code will work perfect. it will scroll down automatic to some position with smooth transition...I hope I got your question right.Limicoline
The thing is that my RecyclerView has like 700 items, so if the distance to the target scroll is < 30, then I use smooth scroll, and this code works perfectly. But if the distance to the target scroll is > 30 I want to automatically jump to that item. In that case I don't want to use smooth scroll. How can I "snap to start" that item?Empoison
@Empoison I have the same question. I believe that you want to change the action to use jumpTo() There used to be an instantaneous scroller but that was deprecated at api 22Townsend
@AlexanderN. Nice I didn't know about that method. Gonna try it ASAP.Empoison
@Luke i did similar but i am getting this error java.lang.NullPointerException: Attempt to read from field 'androidx.recyclerview.widget.RecyclerView$ViewFlinger androidx.recyclerview.widget.RecyclerView.mViewFlinger' on a null object reference at androidx.recyclerview.widget.RecyclerView$SmoothScroller.start(RecyclerView.java:11795) at androidx.recyclerview.widget.RecyclerView$LayoutManager.startSmoothScroll(RecyclerView.java:8470)Donets
P
6

This is the Kotlin code snippet but you can just get the point for scrolling to the item by position properly. The point is to declare the member variable for the layout manager and use its method to scroll.

lateinit var layoutManager: LinearLayoutManager

fun setupView() {
    ...

    layoutManager = LinearLayoutManager(applicationContext)
    mainRecyclerView.layoutManager = layoutManager

    ...
}

fun moveToPosition(position: Int) {
    layoutManager.scrollToPositionWithOffset(position, 0)
}
Perugia answered 19/2, 2020 at 20:40 Comment(0)
L
5

If you want to scroll to a specific position and that position is the adapter's position, then you can use StaggeredGridLayoutManager scrollToPosition

   StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
   staggeredGridLayoutManager.scrollToPosition(10);
   recyclerView.setLayoutManager(staggeredGridLayoutManager);
Lauryn answered 17/4, 2018 at 13:26 Comment(0)
P
0

try recycler_view.smoothScrollBy(0, 100); here 0 represents x-coordinate and 100 represents y-coordinate I used this in vertical recyclerView where I wanted to scroll to next position in the vertical list and for coming to the previous position I simply replaced 100 with -100

Penneypenni answered 5/7, 2018 at 12:16 Comment(0)
M
0

this API is for you.

int offset = 0; recyclerView.scrollToPositionWithOffset(position, offset);

Merozoite answered 11/1 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.