1

I want to use right and left buttons to scroll items with like 10 items

I tried to use scrollBy but it did not work

Here is my Layout Image.

i want to use right and left buttons to scroll I want to be able to scroll right and left

Zain
  • 37,492
  • 7
  • 60
  • 84
Mohamed Farahat
  • 751
  • 1
  • 7
  • 12

2 Answers2

3

You can use for Next button:

mRecyclerView.getLayoutManager().scrollToPosition(linearLayoutManager.findLastVisibleItemPosition() + 1);

And for Previous Button:

mRecyclerView.getLayoutManager().scrollToPosition(linearLayoutManager.findFirstVisibleItemPosition() - 1);

Have look this

Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27
1

You can have a variable that tracks the current displayed position of the RecyclerView, named it "currentPosition" in below snippets

Then increment it if you want to navigate to the next item as follows:

private void scrollToNext() {
    if ((recyclerView.getLayoutManager()) != null) {
        ((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(currentPosition++, 0);
    }
}

You can use RelativeLayout or the custom layout instead of LinearLayout; according to what you're using in your RecyclerView

Similarly, decrement it if you want the previous one

private void scrollToPrevious() {
    if ((recyclerView.getLayoutManager()) != null) {
        ((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(currentPosition--, 0);
    }
}
Zain
  • 37,492
  • 7
  • 60
  • 84