简体   繁体   English

Android Recycler 视图滚动到底部

[英]Android Recycler view scroll to bottom

I am trying to scroll the recycler view to the bottom but the problem is if my row item height is greater that the screen height the scrolling stops at the top of the item.我试图将回收器视图滚动到底部,但问题是如果我的行项目高度大于屏幕高度,滚动停止在项目顶部。 Is it possible to manually scroll to the very bottom of a recycler view?是否可以手动滚动到回收站视图的最底部?

recyclerView.scrollToPosition(adapter.getItemCount()-1); // does not work

it will work for you.它会为你工作。 Use setReverseLayout=true in your LayoutManager and set this to your recylcerView .在您的LayoutManager 中使用setReverseLayout=true并将其设置为您的recylcerView

final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
    linearLayoutManager.setReverseLayout(true);

    recyclerView.setLayoutManager(linearLayoutManager);

This solution will work, 此解决方案将有效,

Add a NestedScrollView as parent, 添加一个NestedScrollView作为父对象,

ie, in your Xml 即在您的Xml中

<android.support.v4.widget.NestedScrollView
 android:id="@+id/nsView"
 android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

<android.support.v4.widget.SwipeRefreshLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
  <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</android.support.v4.widget.SwipeRefreshLayout>
 </android.support.v4.widget.NestedScrollView>

Now in your Java class, after adding the data to recyclerview,make the NestedScrollView to scroll to bottom. 现在在您的Java类中,将数据添加到recyclerview之后,使NestedScrollView滚动到底部。

nsView.fullScroll(View.FOCUS_DOWN); nsView.fullScroll(View.FOCUS_DOWN);

hope, it may help you. 希望对您有帮助。

In java actually lists are zero based.在java中实际上列表是基于零的。 Eg list [0,1,2] has a size of 3 but the last position is 2 because it starts with 0.例如,列表 [0,1,2] 的大小为 3,但最后一个位置是 2,因为它从 0 开始。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM