Search code examples
androidandroid-layoutandroid-activityandroid-recyclerview

How to scroll horizontal RecyclerView programmatically?


I have a horizontal RecyclerView and two button (Next,Previous) as shown in the image below.

enter image description here

so i need to move to the next item or position by use these buttons , i know about method called scrollTo but i don't know how does it work


Solution

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