I would like to scroll by a certain amount of pixels in y direction in my recyclerview. I know that there are methods like scrollToPosition()
or scrollToPositionWithOffset()
. However, those don't really fit my needs.
I saw that LayoutManager
provides a method scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state)
. That sounds like what I want.
How do I use it? What are recycler
and state
? I didn't find any examples online, so some code would be useful.
This wear-specific implementation of LinearLayoutManager provides basic offsetting logic for updating child layout. A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user.
A RecyclerView. LayoutManager implementations that lays out items in a grid. This wear-specific implementation of LinearLayoutManager provides basic offsetting logic for updating child layout.
A ViewHolder describes an item view and metadata about its place within the RecyclerView. RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive View. findViewById(int) results.
Components of a RecyclerView RecyclerView provides these built-in layout managers: LinearLayoutManager shows items in a vertical or horizontal scrolling list. GridLayoutManager shows items in a grid. StaggeredGridLayoutManager shows items in a staggered grid.
I don't know about scrollVerticallyBy
, that looks more like a internal method for the RecyclerView
.
Aren't you just looking for the methods scrollBy(int x, int y)
and smoothScrollBy(int dx, int dy)
?
From the documentation:
public void scrollBy (int x, int y)
Move the scrolled position of your view. This will cause a call to onScrollChanged(int, int, int, int) and the view will be invalidated.
Parameters
x int: the amount of pixels to scroll by horizontally
y int: the amount of pixels to scroll by vertically
You can use the methods like so:
recyclerView.scrollBy(0, 50); //Scroll 50 pixels down
recyclerView.scrollBy(0, -50); //Scroll 50 pixels up
The same goes for smoothScrollBy
.
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