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

AndroidScrollAndroid Recyclerview

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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).

Solution 2 - Android

Below link might solve your problem:

https://stackoverflow.com/a/43505830/4849554

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);

Solution 3 - Android

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);

Solution 4 - Android

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)
}

Solution 5 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncryyyyyView Question on Stackoverflow
Solution 1 - AndroidyugidroidView Answer on Stackoverflow
Solution 2 - AndroidRiteshView Answer on Stackoverflow
Solution 3 - AndroidZeeshanView Answer on Stackoverflow
Solution 4 - AndroidCastorView Answer on Stackoverflow
Solution 5 - Androidrajat singhView Answer on Stackoverflow