[英]Recyclerview inside NestedScrollView - Scroll to bottom
我在NestedScrollView
中有一个RecyclerView
:
<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>
我的RecyclerView
中充满了onCreate()
中的项目
在设备上,您会在最顶部看到RecyclerView
的第一项,并且必须向下滚动NestedScrollView
才能看到最后一项。
由于我的项目是按发送时间排序的聊天消息,我需要NestedScrollView
一直向下滚动,这样用户就可以首先看到最新的聊天消息,而不必首先滚动。
对此有什么想法吗?
鉴于您的RecyclerView
是NestedScrollView
的唯一子NestedScrollView
,您最好完全删除NestedScrollView
,然后将固定高度应用于RecyclerView
。 像这样:
<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" />
这样做可以让RecyclerView本身管理滚动而不是父滚动视图。 这样 ,您就可以利用LinearLayoutManager
的属性来实现所需的功能。
反向布局-设置此选项将“反转”您的列表; 适配器中的第一项将出现在列表的底部, RecyclerView
的默认滚动位置将一直滚动到底部。
LinearLayoutManager lm = new LinearLayoutManager(this);
lm.setReverseLayout(true);
如果您有同样的问题并希望保留 NestedScrollView。 它会像这样工作。
Handler(Looper.getMainLooper()).postDelayed({
binding.nestedScrollView.smoothScrollTo(
0,
binding.recyclerview.measuredHeight,
500
)
// binding.nestedScrollView.scrollTo(0, binding.recyclerview.measuredHeight)
// binding.nestedScrollView.smoothScrollTo(0, binding.recyclerview.measuredHeight)
}, 50L)
对我来说,它没有立即起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.