Is there a RecyclerView equivalent to ListView's transcriptMode alwaysScroll?
Asked Answered
R

2

4

With ListView, I could easily implement an auto-scrolling chat view by setting:

android:transcriptMode="alwaysScroll"

in its XML. Is there an equivalent in RecyclerView?

http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:transcriptMode

Rampageous answered 28/1, 2015 at 1:41 Comment(0)
X
5

RecyclerView doesn't seem to have that option. You have to manually scroll. Do something like....

notifyItemInserted(int posn);
recyclerView.smoothScrollToPosition(la.getItemCount()); // <= use this.

Also void notifyItemInserted(int posn); is final. So you can't Override that to always scroll there either.

You need to make a method where you can call something similar to the above code as bundles.

Also keep in mind smooth scroll is kinda buggy. If you have many elements(e.g. 1000 elements) and you want to scroll a long distance, the scroll takes forever. There's a tutorial I found that addresses this issue and explains how you can fix that. http://blog.stylingandroid.com/scrolling-recyclerview-part-1/

Enjoy,

Ximenes answered 27/2, 2015 at 20:27 Comment(1)
Not la.getItemCount() - 1?Royston
A
2

In my case, I add OnLayoutChangeListener to RecyclerView, because when keyboard is hidden or showed, the RecyclerView's bottom position will be changed. Codes like this:

recyclerView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight,
            int oldBottom) {
        if (bottom < oldBottom) {
            recyclerView.post(new Runnable() {
                @Override
                public void run() {
                    recyclerView.scrollToPosition(recyclerView.getAdapter().getItemCount() - 1);
                }
            });
        }
    }
});

And it works the same as set android:transcriptMode="alwaysScroll" to ListView.

Avar answered 27/4, 2017 at 2:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.