RecyclerView聊天界面从底部显示 和 倒序

本文详细介绍如何使用LinearLayoutManager在RecyclerView中实现数据的倒序显示,并保持从最后一项数据开始显示的效果。文中提供了两种实现方法,一是使用scrollToPosition方法,二是通过设置LinearLayoutManager的stackFromEnd属性。

通过管理器改变排列方向和展示数据的顺序

LinearLayoutManager(Context context, int orientation, boolean reverseLayout);

在这里插入图片描述context: Fragment用getActivity()获取当前上下文;Activity用xxxActivity.this。或其它方式获取上下文。
orientation: 垂直排列:LinearLayoutManager.VERTICAL 或 水平排列 LinearLayoutManager.HORIZONTAL
reverseLayout: true表示倒序展示数据,false表示正序展示数据。

例如:倒序展示数据列表。

mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), 
									LinearLayoutManager.VERTICAL, true));

上面方法虽然将数据倒序显示出来,但是默认定位到第一条数据,需要从下往上翻。所以,

默认从最后一条数据开始显示

方法一:

mRecycleView.scrollToPosition(adapter.getItemCount-1);

方法二

LinearLayoutManager manager = new LinearLayoutManager(xxxActivity.this, 
										LinearLayoutManager.VERTICAL, true);
manager.setStackFromEnd(true);
mRecyclerView.setLayoutManager(manager);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值