Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use scrollVerticallyBy() with Android RecyclerView and LayoutManager?

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.

like image 662
user2426316 Avatar asked Jun 06 '15 13:06

user2426316


People also ask

What is LayoutManager in RecyclerView?

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.

What is the use of linear layout manager?

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.

What is ViewHolder in RecyclerView Android?

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.

What are different types of layout manager can RecyclerView use to give different directions for items?

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.


1 Answers

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.

like image 92
rubengees Avatar answered Oct 07 '22 03:10

rubengees