RecyclerView vertical scroll in pixels in Android

In my chat application, I use RecyclerView and LayoutManager to display a list of chat messages. I have a case where a user views old messages and a new message arrives. At this point, I want to scroll through the chat a short distance to confirm the user of the received new message. I need to scroll my RecyclerView a distance in pixels. I found that layoutManager has a scrollVerticallyBy method,

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

But I'm confused about the required parameters, RecyclerView.Recycler recycler, RecyclerView.State state , and I'm not sure that it will do my job.

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

+3
source share
2 answers

You can use:

recyclerView.smoothScrollToPosition(recyclerView.getAdapter().getItemCount());

if ant is a relevant position.

 recyclerView.smoothScrollToPosition(0);

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

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

LinearSmoothScroller, . - . , , . , , , , 10000 25 , , .

+2

- :

recyclerView.smoothScrollBy(0, 100);

. x y:

public void smoothScrollBy(int dx, int dy)

. smothScrollBy(dx,dy) , - , RecyclerView . :

new Handler().postDelayed(new Runnable() {
  @Override public void run() {
    recyclerView.smoothScrollBy(0, 100);
  }
}, 200);

, ,

+3

Source: https://habr.com/ru/post/1654518/


All Articles