I have a RecyclerView
inside a NestedScrollView
:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="@dimen/_100sdp">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_chat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/_100sdp"
android:layout_marginLeft="@dimen/_30sdp"
android:layout_marginRight="@dimen/_30sdp"
android:background="@color/bright_grey"
></android.support.v7.widget.RecyclerView>
</android.support.v4.widget.NestedScrollView>
My RecyclerView
is being filled with items in onCreate()
On a device you would see the first item of the RecyclerView
on the very top und would have to scroll down the NestedScrollView
in order to see the last item.
Since my items are chat message sorted by the time sent I need the NestedScrollView
to be scrolled all the way down so users would see the latest chat message first without having to scroll in the first place.
Any ideas on this?
Given that your RecyclerView
is the only child of your NestedScrollView
, you would be better off removing the NestedScrollView
altogether, and instead applying the fixed height to the RecyclerView
. Something like this:
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_chat"
android:layout_width="match_parent"
android:layout_height="@dimen/_100sdp"
android:layout_marginLeft="@dimen/_30sdp"
android:layout_marginRight="@dimen/_30sdp"
android:background="@color/bright_grey" />
Doing this allows you to have the RecyclerView itself manage scrolling, rather than the parent scroll view. And that allows you to leverage a property of LinearLayoutManager
to achieve what you want.
Reverse layout -- setting this will "invert" your list; the first item in your adapter will appear at the bottom of the list, and the default scroll position of the RecyclerView
will be to scroll all the way to the bottom.
LinearLayoutManager lm = new LinearLayoutManager(this);
lm.setReverseLayout(true);