保存嵌套RecyclerView的滚动位置
创始人
2024-11-22 15:30:19
0

要保存嵌套RecyclerView的滚动位置,可以使用以下步骤:

  1. 在父RecyclerView的LayoutManager中保存子RecyclerView的滚动位置。
  2. 在父RecyclerView的onSaveInstanceState方法中保存子RecyclerView的滚动位置。
  3. 在父RecyclerView的onRestoreInstanceState方法中恢复子RecyclerView的滚动位置。

以下是一个示例代码:

public class ParentRecyclerView extends RecyclerView {
    private LinearLayoutManager mLayoutManager;
    private SparseArray mSavedStateArray = new SparseArray<>();

    public ParentRecyclerView(Context context) {
        super(context);
        init();
    }

    public ParentRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ParentRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        mLayoutManager = new LinearLayoutManager(getContext());
        setLayoutManager(mLayoutManager);
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        Bundle state = new Bundle();
        state.putParcelable("superState", super.onSaveInstanceState());

        // 保存子RecyclerView的滚动位置
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            if (childView instanceof ChildRecyclerView) {
                int position = getChildAdapterPosition(childView);
                Parcelable savedState = mLayoutManager.onSaveInstanceState();
                mSavedStateArray.put(position, savedState);
            }
        }

        state.putSparseParcelableArray("childStates", mSavedStateArray);
        return state;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state instanceof Bundle) {
            Bundle savedState = (Bundle) state;
            super.onRestoreInstanceState(savedState.getParcelable("superState"));

            // 恢复子RecyclerView的滚动位置
            mSavedStateArray = savedState.getSparseParcelableArray("childStates");
            for (int i = 0; i < mSavedStateArray.size(); i++) {
                int position = mSavedStateArray.keyAt(i);
                Parcelable childState = mSavedStateArray.get(position);
                View childView = mLayoutManager.findViewByPosition(position);
                if (childView instanceof ChildRecyclerView) {
                    ((ChildRecyclerView) childView.getLayoutManager()).onRestoreInstanceState(childState);
                }
            }
        } else {
            super.onRestoreInstanceState(state);
        }
    }
}

public class ChildRecyclerView extends RecyclerView {
    private LinearLayoutManager mLayoutManager;

    public ChildRecyclerView(Context context) {
        super(context);
        init();
    }

    public ChildRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ChildRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        mLayoutManager = new LinearLayoutManager(getContext());
        setLayoutManager(mLayoutManager);
    }
}

在示例中,ParentRecyclerView是父RecyclerView,ChildRecyclerView是子RecyclerView。在ParentRecyclerViewonSaveInstanceState方法中,保存了子RecyclerView的滚动位置,并在onRestoreInstanceState方法中恢复了子RecyclerView的滚动位置。

相关内容

热门资讯

4总结(aapoker开发)外... 4总结(aapoker开发)外挂透明挂辅助神器(软件透明挂)透视辅助(2025已更新)(哔哩哔哩);...
ai代打(wpk微扑克)其实真... WePoker透视辅助工具核心要点解析‌,ai代打(wpk微扑克)其实真的有挂!其实真的有挂(202...
(WPk透明挂)确实有挂-AA... (WPk透明挂)确实有挂-AAPoKER透明挂透视辅助-fishpoker app辅助;亲们利用一分...
5分钟实锤!微友麻将外挂透视辅... 自定义系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管你是想分享给你好...
第3工具(wpk安装)外挂透明... 第3工具(wpk安装)外挂透明挂辅助挂(透视)透视辅助(2022已更新)(哔哩哔哩);原来确实真的有...
线上(aapoker有外挂)确... aapoker有外挂赢率提升策略‌;线上(aapoker有外挂)确实真的有挂!原来真的有挂(2023...
七辅助挂(poker worl... 七辅助挂(poker world外挂)外挂透明挂辅助机制(辅助挂)透视辅助(2023已更新)(哔哩哔...
(WPk透明挂)有挂教学-WP... (WPk透明挂)有挂教学-WPK技巧辅助透视-红龙扑克有挂;支持多人共享记分板与复盘,通过邀请好友或...
九分钟普及!同城字牌软件透明挂... 九分钟普及!同城字牌软件透明挂辅助-同城字牌其实真的有挂(2023已更新)1、ai机器人多个强度级别...
第2私人房(Wepoke挂)外... 您好,Wepoke挂这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这...