10

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 replacement for ListView.smoothScrollBy(int distance, int duration)

Bugs Happen
  • 2,169
  • 4
  • 33
  • 59
Rafael
  • 6,091
  • 5
  • 54
  • 79

3 Answers3

11

The best way to achieve this is using this:

recyclerView.smoothScrollBy(0, 100);

This is the signature of the method. You can scroll in x and y axis:

public void smoothScrollBy(int dx, int dy)

Note: If smothScrollBy(dx,dy) does not work is due to the RecyclerView has not been already loaded with its elements. For that I would recomend using:

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

In that way, you can be sure that the views have been loaded

Antonio
  • 11,413
  • 6
  • 34
  • 48
  • What if I don't want that smooth scrolling, I just want to scroll it to that axis point directly, what do I use in that case? RecyclerView.scrollBy() doesn't seem to work. – Sagar Dec 07 '17 at 09:56
  • @Sagar , have you checked if the elements were already loaded when you call the method? Follow my suggestion at the end of the answer – Antonio Dec 07 '17 at 10:17
  • 1
    Thanks, that helped! – Sagar Dec 07 '17 at 11:22
1

You can use:

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

if ant 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);
}

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.

NovusMobile
  • 1,813
  • 2
  • 21
  • 48
  • 6
    I want to scroll by distance, not to the end or start of the list. – Rafael May 15 '15 at 09:53
  • I modify my answer, this is the best pixcel form that you use. – NovusMobile May 15 '15 at 10:00
  • so for example I have 100 messages in list, and I am somewhere in the middle of the list. I want to scroll by one message down, how I can use your code? – Rafael May 15 '15 at 10:27
  • read the second parth for this url. your query will be already answered there. – NovusMobile May 15 '15 at 10:48
  • as I understood from article, we can't scroll by distance in RecyclerView – Rafael May 15 '15 at 11:36
  • This important for you? – NovusMobile May 18 '15 at 08:58
  • I think we can use alternative way to notify user about new messages, instead of scrolling by a little distance – Rafael May 18 '15 at 12:17
  • I will give you one more in that .. if new message will come , move to selection there , so automatically user will get the new message. accept answer if it near help to you.It will help others if they will face this type of issue. – NovusMobile May 18 '15 at 12:20
  • Hey @NovusMobile can you please help me with this https://stackoverflow.com/questions/49952965/recyclerview-horizontal-scrolling-to-left?noredirect=1#comment87861669_49952965 –  May 19 '18 at 08:47
0

You can scroll to the bottom of the recycler view directly like this without using smoothScroll.

/* Method to set the recycler view focus to bottom */
private void scrollToBottom() {
    mRecyclerView.scrollToPosition(mAdapter.getItemCount() - 1);
}