Rafael Muhamedzyanov Rafael Muhamedzyanov - 1 year ago 123
Android Question

Vertical scroll the RecyclerView in Android by pixels

In my chat app I use RecyclerView and LayoutManager for showing chat messages list. I have a case when user browse old messages and new message arrives. At that point I want to scroll chat by small distance to acknowledge user of received new message.
I need to scroll my RecyclerView by distance in pixels.
I found that layoutManager has method scrollVerticallyBy,

public int scrollVerticallyBy (int dy, RecyclerView.Recycler recycler, RecyclerView.State state)

But I got confused by parameters it requires, RecyclerView.Recycler recycler, RecyclerView.State state ,
and I am not sure if it will do my job.

In other words, I want to find replcament for ListView.smoothScrollBy(int distance, int duration);

Answer Source

You can use:


if ant relevant position .


ref : http://blog.stylingandroid.com/scrolling-recyclerview-part-1/

public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
        int position) {
    LinearSmoothScroller linearSmoothScroller =
            new LinearSmoothScroller(recyclerView.getContext()) {
                public PointF computeScrollVectorForPosition(int targetPosition) {
                    return LinearLayoutManager.this

The instance of LinearSmoothScroller which is responsible for performing the actual scroll. This isn’t a trivial class – it contains an awful lot of logic. However the behaviour that it implements is fairly simple to understand. Rather than try and determine the exact number of pixels that it needs to scroll through before reaching the end point, it performs a series of 10000 pixel flings at a speed of 25 milliseconds per inch until the end target item comes in to the viewport, and then it decelerates to stop with the end target visible.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download