16

I have a simple project displaying user text messages. I am currently not able to scroll the RecyclerView to the last received text message.
My recyclerView is inside a very simple activity using the Coordinator layout:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@color/background"
tools:context="com.myproject.myproject.MessageActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_message" />
</android.support.design.widget.CoordinatorLayout>

And the content_message.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.textbutler.textbutler.MessageActivity"
tools:showIn="@layout/activity_message">

<!-- some views here -->

<android.support.v7.widget.RecyclerView
    android:id="@+id/content_text"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:scrollbars="vertical" />

<!-- some views here -->
</LinearLayout>

The activity load the data through a basic loader. I force the access to the recyclerview on the UI thread and with a delay (just in case)

public class MessageActivity extends AppCompatActivity {
private static final int URL_LOADER = 0;

private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private MessageAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_message);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    initializeList(savedInstanceState);
}

private void initializeList(Bundle savedInstanceState) {
    if (recyclerView == null) {
        recyclerView = (RecyclerView) findViewById(R.id.content_text);

        if (recyclerView == null)
            return;

        if (layoutManager == null) {
            layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
            layoutManager.setReverseLayout(true);
            layoutManager.setStackFromEnd(true);
        }

        recyclerView.setLayoutManager(layoutManager);

        final Activity me = this;

        if (adapter == null) {
            adapter = new MessageAdapter(new ObservableCallback() {
                @Override
                public void callback() {
                    // this function is called right after the recyclerview received notifyDataSetChanged
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            me.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Log.d("ScrollingUI", "scrolltoposition " + adapter.getItemCount());
                                    recyclerView.scrollToPosition(adapter.getItemCount() - 1);
                                }
                            });
                        }
                    }, 1000);
                }
            });
            SMSByUserLoader loader = new SMSByUserLoader(this, adapter);
            getSupportLoaderManager().initLoader(URL_LOADER, savedInstanceState, loader);
        }

        recyclerView.setAdapter(adapter);
    }
}

}

When I run this code, the RecyclerView is perfectly filled. After a second I have the correct log but the view does not changed. I already found various answers to this issue, but none of them seems to work in my case.
Scrolling in the RecyclerView perfectly works.

Bathilde Rocchia
  • 263
  • 1
  • 3
  • 10

7 Answers7

15

I have the same problem and smoothScrollToPosition method solve it:

 RecyclerView.smoothScrollToPosition(postion);
Luiz Fernando Salvaterra
  • 4,192
  • 2
  • 24
  • 42
9

Inspired by @omid-jafarinezhad, I need to use smoothScrollToPosition, but I still need it instant scroll to the end because it will looks weird if keep scrolling many items slowly. And my workaround is use both methods at sequence, i.e.:

        myRecyclerView.scrollToPosition(position);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                myRecyclerView.smoothScrollToPosition(position);
            }
        }, 50); //sometime not working, need some delay

This may seems like a hack but at least solve my problem at this time.

林果皞
  • 7,539
  • 3
  • 55
  • 70
7

You used layoutManager.setReverseLayout(true) and layoutManager.setStackFromEnd(true) in order to start your list from the end. So, if I understand it correctly, the list starts now from position adapter.getItemCount() - 1 and your list shouldn't scroll. Maybe you should try ...scrollToPosition(0).

user35603
  • 765
  • 2
  • 8
  • 24
7

My problem was that my RecyclerView was inside a NestedScrollView. I removed the NestedScrollView, and scrollToPosition() worked.

Jack'
  • 1,722
  • 1
  • 19
  • 27
1
 private val delayShowRunnable by lazy{
    object:Runnable{
        override fun run() {
            rv_content?.alpha=1f
        }
    }
}

private   val  linearSmoothScroller by lazy{
   object: LinearSmoothScroller(context){

         override fun onTargetFound(targetView: View, state: RecyclerView.State, action: Action) {
           super.onTargetFound(targetView, state, action)
           rv_content?.postDelayed(delayShowRunnable,50)
       }
       override fun calculateTimeForDeceleration(dx: Int): Int {
           return  1//set max speed
       }

       override fun calculateTimeForScrolling(dx: Int): Int {
           return 1  //set max speed
       }
    }
}

rv_content?.alpha=0f  //hide listview before scroll finish 
 
 linearSmoothScroller.targetPosition = pos
        (rv_content?.layoutManager as LinearLayoutManager?)?.startSmoothScroll(linearSmoothScroller)

scroll position change will not be reflected until the next layout call. so we should use <smoothscrollToPosition()> , find out target view.

HongSec Park
  • 1,193
  • 8
  • 9
1

Always use -1 if you want to scroll to the end of your adapter recyclerView.scrollToPosition(chatAdapter.getItemCount()-1);

Sujith S Manjavana
  • 1,417
  • 6
  • 33
  • 60
0
  1. In a usual case you can wait some time till a RecyclerView can scroll.

    binding.recyclerView.postDelayed({
        binding.recyclerView.scrollToPosition(adapter.itemCount - 1)
    }, 100)
    

Instead of 100 ms you can use numbers from 0 to 1000, depending on a device.

  1. If you use Paging adapter it can request a new page. When you try to scroll a RecyclerView the adapter isn't ready (new items haven't not been added). So you can add a subscriber to request finish and then scroll the list (probably after a pause like in the first case).
CoolMind
  • 26,736
  • 15
  • 188
  • 224