Home » Android » java – How to scroll to the bottom of a RecyclerView? scrollToPosition doesn't work

java – How to scroll to the bottom of a RecyclerView? scrollToPosition doesn't work

Posted by: admin March 10, 2020 Leave a comment


I’d like to scroll to the bottom of the RecyclerView list after loading the activity.

GENERIC_MESSAGE_LIST = (ArrayList) intent.getExtras().getParcelableArrayList(ConversationsAdapter.EXTRA_MESSAGE);
conversationView = (RecyclerView) findViewById(R.id.list_messages);
conversationViewLayoutManager = new LinearLayoutManager(this);
conversationViewAdapter = new ConversationAdapter(GENERIC_MESSAGE_LIST, this);

conversationView.scrollTo(...) throws an exception about being not supported in RecyclerView, and conversationView.scrollToPosition(...) doesn’t seem to do anything.

After the above block of code, I added

conversationView.scrollToPosition(GENERIC_MESSAGE_LIST.size() + 1)

which doesn’t work. There are 30 elements in GENERIC_MESSAGE_LIST.

How to&Answers:

Just set setStackFromEnd=true or setReverseLayout=true so that LLM will layout items from end.

The difference between these two is that setStackFromEnd will set the view to show the last element, the layout direction will remain the same. (So, in an left-to-right horizontal Recycler View, the last element will be shown and scrolling to the left will show the earlier elements)

Whereas setReverseLayout will change the order of the elements added by the Adapter. The layout will start from the last element, which will be the left-most in an LTR Recycler View and then, scrolling to the right will show the earlier elements.


final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());

See documentation for details.


I was looking at this post to find the answer but… I think everyone on this post was facing the same scenario as me: scrollToPosition() was fully ignored, for an evident reason.

What I was using?


… what WORKED?

recyclerView.scrollToPosition(items.size() - 1);


I know its late to answer here, still if anybody want to know solution is below

conversationView.smoothScrollToPosition(conversationView.getAdapter().getItemCount() - 1);


To scrolldown from any position in the recyclerview to bottom

edittext.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                rv.postDelayed(new Runnable() {
                    public void run() {
                      rv.scrollToPosition(rv.getAdapter().getItemCount() - 1);
                }, 1000);


Add this code after sending message and before getting message from server

recyclerView.scrollToPosition(mChatList.size() - 1);


When you call setAdapter, that does not immediately lay out and position items on the screen (that takes a single layout pass) hence your scrollToPosition() call has no actual elements to scroll to when you call it.

Instead, you should register a ViewTreeObserver.OnGlobalLayoutListener (via addOnGlobalLayoutListner() from a ViewTreeObserver created by conversationView.getViewTreeObserver()) which delays your scrollToPosition() until after the first layout pass:

conversationView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  public void onGlobalLayout() {
    // Unregister the listener to only call scrollToPosition once

    // Use vto.removeOnGlobalLayoutListener(this) on API16+ devices as 
    // removeGlobalOnLayoutListener is deprecated.
    // They do the same thing, just a rename so your choice.


Solution for Kotlin:

apply below code after setting “recyclerView.adapter” or after “recyclerView.adapter.notifyDataSetChanged()”

recyclerView.scrollToPosition(recyclerView.adapter.itemCount - 1)


In Kotlin:

recyclerView.viewTreeObserver.addOnGlobalLayoutListener { scrollToEnd() }

private fun scrollToEnd() =
        (adapter.itemCount - 1).takeIf { it > 0 }?.let(recyclerView::smoothScrollToPosition)


class MyLayoutManager extends LinearLayoutManager {

  public MyLayoutManager(Context context) {
    super(context, LinearLayoutManager.VERTICAL, false);

  @Override public void smoothScrollToPosition(RecyclerView recyclerView,
      final RecyclerView.State state, final int position) {

    int fcvip = findFirstCompletelyVisibleItemPosition();
    int lcvip = findLastCompletelyVisibleItemPosition();

    if (position < fcvip || lcvip < position) {
      // scrolling to invisible position

      float fcviY = findViewByPosition(fcvip).getY();
      float lcviY = findViewByPosition(lcvip).getY();

      recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {

        int currentState = RecyclerView.SCROLL_STATE_IDLE;

        @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

          if (currentState == RecyclerView.SCROLL_STATE_SETTLING
              && newState == RecyclerView.SCROLL_STATE_IDLE) {

            // recursive scrolling
            smoothScrollToPosition(recyclerView, state, position);

          currentState = newState;

        @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

          int fcvip = findFirstCompletelyVisibleItemPosition();
          int lcvip = findLastCompletelyVisibleItemPosition();

          if ((dy < 0 && fcvip == position) || (dy > 0 && lcvip == position)) {
            // stop scrolling

      if (position < fcvip) {
        // scroll up

        recyclerView.smoothScrollBy(0, (int) (fcviY - lcviY));
      } else {
        // scroll down

        recyclerView.smoothScrollBy(0, (int) (lcviY - fcviY));
    } else {
      // scrolling to visible position

      float fromY = findViewByPosition(fcvip).getY();
      float targetY = findViewByPosition(position).getY();

      recyclerView.smoothScrollBy(0, (int) (targetY - fromY));


MyLayoutManager layoutManager = new MyLayoutManager(context);

RecyclerView.Adapter adapter = new YourAdapter();

recyclerView.smoothScrollToPosition(adapter.getItemCount() - 1);

above code works, but it’s not smooth and not cool.


IF you have adapter attached with recyclerView then just do this.



Roc answer is a great help. I would like to add a small block to it:

mRecyclerView.scrollToPosition(mAdapter.getItemCount() - 1);


In my case where views do not have the same height, calling scrollToPosition on the LayoutManager worked to really scroll to the bottom and see fully the last item:

recycler.getLayoutManager().scrollToPosition(adapter.getItemCount() - 1);


This works perfectly fine for me:

AdapterChart adapterChart = new AdapterChart(getContext(),messageList);


First time scroll when entering in recycler view first time then use


put in minus for scroll down for scroll up put in positive value);

if the view is very big in height then scrolltoposition particular offset is used for the top of view then you use

int overallXScroldl =chatMessageBinding.rvChat.computeVerticalScrollOffset();
chatMessageBinding.rvChat.smoothScrollBy(0, Math.abs(overallXScroldl));


Only Ian’s answer was able to make my RecyclerView scroll to a specified position. However, The RecyclerView was not able to scroll afterwards when I used scrollToPosition(). smoothScrollToPosition() worked but the initial animation made it too slow when the list was long.
The reason was the listener was not removed. I used the code below to remove the current ViewTreeObserver and it worked as a charm.

    mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {


this code will give you latest post first, i think this answer is helpful.


    LinearLayoutManager layoutManager = new LinearLayoutManager(this);



Tried a method of @galex, it worked until refactoring. So I used an answer of @yanchenko and changed a bit. Probably this is because I called scrolling from onCreateView(), where a fragment view was built (and probably didn’t have right size).

private fun scrollPhotosToEnd(view: View) {
    view.recycler_view.viewTreeObserver.addOnGlobalLayoutListener(object :
        ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            } else {
            adapter?.itemCount?.takeIf { it > 0 }?.let {
                view.recycler_view.scrollToPosition(it - 1)

You can also add a check of viewTreeObserver.isAlive like in https://stackoverflow.com/a/39001731/2914140.