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

假如想象 提交于 2020-01-11 06:01:07

问题


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


回答1:


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,




回答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.



来源:https://stackoverflow.com/questions/28183248/is-there-a-recyclerview-equivalent-to-listviews-transcriptmode-alwaysscroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!