How to Scroll Recyclerview Programmatically

How to scroll recyclerView programmatically?

You can use scrollToPosition()

recyclerView.post(new Runnable() {
@Override
public void run() {
recyclerView.scrollToPosition(adapter.getItemCount() - 1);
// Here adapter.getItemCount()== child count
}
});

or smoothScrollToPosition()

recyclerView.post(new Runnable() {
@Override
public void run() {
recyclerView.smoothScrollToPosition(adapter.getItemCount()- 1);
}
});

To move up again, you need to call the above method with index 0. But first, you need to make sure that the RecyclerView is scrolled to last.
So, put a ScrollListener on RecyclerView to make sure the last item is visible.

Android RecyclerView scroll to programmatically not working

Did you try with LinearLayoutManager method scrollToPositionWithOffset

//position 2 and second param is offset 20
mLayoutManager.scrollToPositionWithOffset(2, 20);

this works for me. Hope to work and for you !

How to scroll horizontal RecyclerView programmatically?

I found the answer:

case R.id.next:
mRecyclerView.getLayoutManager().scrollToPosition(linearLayoutManager.findLastVisibleItemPosition() + 1);
break;
case R.id.pre:
mRecyclerView.getLayoutManager().scrollToPosition(linearLayoutManager.findFirstVisibleItemPosition() - 1);
break;

How to scroll the RecyclerView programatically by a specific pixels?

As recyclerView.smoothScrollBy(0, pixels); is not working for your custom view you can try an alternative way.

What you can do is scroll by position by doing some Math but it wont be exact to the pixel.

Hypothetically speaking if your Items are of equal height of 100dp you can convert dp to pixels for any screen type by code see here

Lets say 100dp comes to 100px per item and you want to scroll 400px down the Recycler. That's 4 positions (400 / 100).

All you need then is the current in View bottom item position number, add 4 and scroll or smooth scroll by position.

Here is a helper class to show you how to get the Top or Bottom item position in View if you wish to go both up or down the Recycler.

For a complete Solution Check here

Programmatically scroll a RecyclerView with GridLayoutManager

Actually:

mRecyclerView.getLayoutManager().scrollToPosition(0);

works.
In my case it wasn't because I was calling it synchronously in the method:

public void onDestroyActionMode(ActionMode mode)

It was enough to post a runnable (without any delay) to the looper of the main thread.

    new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (mRecyclerView != null) {
mRecyclerView.scrollToPosition(0);
}
}
});

How to scroll to the bottom of a RecyclerView? scrollToPosition doesn't work

Just set setStackFromEnd=true or setReverseLayout=true so that LLM will layout items from end.

The difference between these two is that setStackFromEnd will set the view to show the last element, the layout direction will remain the same. (So, in an left-to-right horizontal Recycler View, the last element will be shown and scrolling to the left will show the earlier elements)

Whereas setReverseLayout will change the order of the elements added by the Adapter. The layout will start from the last element, which will be the left-most in an LTR Recycler View and then, scrolling to the right will show the earlier elements.

Sample:

final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setReverseLayout(true);
_listView.setLayoutManager(linearLayoutManager);

See documentation for details.



Related Topics



Leave a reply



Submit