美文网首页
Android记录调用Adapter的刷新后保持RecycleV

Android记录调用Adapter的刷新后保持RecycleV

作者: 坑逼的严 | 来源:发表于2022-01-19 17:15 被阅读0次

当我们给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);

相关文章

网友评论

      本文标题:Android记录调用Adapter的刷新后保持RecycleV

      本文链接:https://www.haomeiwen.com/subject/rigrhrtx.html