Android记录调用Adapter的刷新后保持RecycleView位置

当我们给recycleview的布局设置从底部展示,高度是match_parent的时候

linearLayoutManager.setStackFromEnd(true);

那么数据过少的时候,他是排版在底部的。解决这个问题,我们需要把它设置成wrap_content。比如:

<com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/mSmartRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/mRlTitle"
        android:layout_above="@id/mLlButtom">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/mRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

但是当我们调用notifyItemRangeInserted向列表插入数据时,recycleview会往上滑动。
看了篇博客说把高度设置成match_parent,这。。。。。。这不就是坑爹吗?怎么办,肯定不能这么搞。然后就找有没有保持当前状态不滑动的代码。这里记录下,怕忘了

Parcelable parcelable = mRecyclerView.getLayoutManager().onSaveInstanceState();
if(mAdapter != null){
   mAdapter.addData(messageList);
}
 mRecyclerView.getLayoutManager().onRestoreInstanceState(parcelable);
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容