I have a horizontal RecyclerView
and two button (Next,Previous) as shown in the image below.
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
You can use scrollToPosition() with the index of the last position.
LinearLayoutManager shows items in a vertical or horizontal scrolling list. GridLayoutManager shows items in a grid. StaggeredGridLayoutManager shows items in a staggered grid.
RecyclerView is the ViewGroup that contains the views corresponding to your data. It's a view itself, so you add RecyclerView into your layout the way you would add any other UI element. Each individual element in the list is defined by a view holder object.
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;
RecyclerViews
have a methods which they expose for scrolling to a certain position:
Snap scroll to a given position:
mRecyclerView.scrollToPosition(int position)
Smooth scroll to a given position:
mRecyclerView.smoothScrollToPosition(int position)
For these methods to work, the LayoutManager
of the RecyclerView
needs to have implemented these methods, and LinearLayoutManager
does implement these in a basic manner, so you should be good to go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With