简体   繁体   中英

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 .

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

    recyclerView.setLayoutManager(linearLayoutManager);

This solution will work,

Add a NestedScrollView as parent,

ie, in your 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.

nsView.fullScroll(View.FOCUS_DOWN);

hope, it may help you.

In java actually lists are zero based. Eg list [0,1,2] has a size of 3 but the last position is 2 because it starts with 0.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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