Android嵌套RecyclerView与LinearLayoutManager和FlexboxLayoutManager无法正常工作
创始人
2024-10-09 15:35:22
0

要在给定的嵌套RecyclerView中实现LinearLayoutManager和FlexboxLayoutManager,需要使用自定义LayoutManager类ChildLayoutManager来实现。 在ChildLayoutManager类中,通过重写canScrollVertically()和scrollVerticallyBy()方法来处理嵌套RecyclerView的滚动。以下是示例代码:

public class ChildLayoutManager extends LinearLayoutManager {
    private int[] mMeasuredDimension = new int[2];

    public ChildLayoutManager(Context context) {
        super(context);
    }

    public ChildLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    @Override
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
                          int widthSpec, int heightSpec) {

        final int widthMode = View.MeasureSpec.getMode(widthSpec);
        final int heightMode = View.MeasureSpec.getMode(heightSpec);
        final int widthSize = View.MeasureSpec.getSize(widthSpec);
        final int heightSize = View.MeasureSpec.getSize(heightSpec);

        int width = 0;
        int height = 0;
        for (int i = 0; i < getItemCount(); i++) {
            if (getOrientation() == HORIZONTAL) {
                measureScrapChild(recycler, i,
                        View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                        heightSpec,
                        mMeasuredDimension);

                width = width + mMeasuredDimension[0];
                if (i == 0) {
                    height = mMeasuredDimension[1];
                }
            } else {
                measureScrapChild(recycler, i,
                        widthSpec,
                        View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                        mMeasuredDimension);
                height = height + mMeasuredDimension[1];
                if (i == 0) {
                    width = mMeasuredDimension[0];
                }
            }
        }

        switch (widthMode) {
            case View.MeasureSpec.EXACTLY:
                width = widthSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }

        switch (heightMode) {
            case View.MeasureSpec.EXACTLY:
                height = heightSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }

        setMeasuredDimension(width, height);
    }

    private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
                                   int heightSpec,
                                   int[] measuredDimension) {
        View view = recycler.getViewForPosition(position);
        if (view != null) {
            RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
            int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
                    getPaddingLeft() + getPaddingRight(), p.width);
            int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
                    getPaddingTop() + getPaddingBottom(), p.height);
            view.measure(childWidthSpec, childHeightSpec);
            measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
            measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
            recycler.recycleView(view);
        }
    }

    @Override
    public boolean canScrollVertically() {
        return false;
    }

    @Override
    public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
        return 0;
    }
}

相关内容

热门资讯

六分钟了解!哈灵辅助工具,快玩... 六分钟了解!哈灵辅助工具,快玩炸翻天app辅助,安装教程(有挂介绍)1、完成快玩炸翻天app辅助的残...
3分钟了解!黄冈麻将有挂吗,斗... 3分钟了解!黄冈麻将有挂吗,斗棋联盟辅助器,技巧教程(有挂攻略)1、进入到斗棋联盟辅助器黑科技之后,...
4分钟了解!财神十三张什么机制... 4分钟了解!财神十三张什么机制,广西老友麻将有挂的吗,2025版教程(有挂秘籍);暗藏猫腻,小编详细...
6分钟了解!钱塘十三水有挂是真... 6分钟了解!钱塘十三水有挂是真的吗,蜀州麻将有挂没,黑科技教程(有挂脚本)1、许多玩家不知道钱塘十三...
1分钟了解!皮皮四川麻将,皮皮... 1分钟了解!皮皮四川麻将,皮皮麻将真的有挂吗,必赢方法(有挂黑科技)1、皮皮麻将真的有挂吗系统规律教...
八分钟了解!老铁联盟牛牛辅助器... 八分钟了解!老铁联盟牛牛辅助器,边锋老友内蒙古麻将挂,安装教程(有挂攻略)1、边锋老友内蒙古麻将挂透...
二分钟了解!江苏大宝苏北麻将有... 二分钟了解!江苏大宝苏北麻将有挂吗,正版欢乐达人辅助工具,2025新版教程(有挂软件);1、很好的工...
八分钟了解!越乡游义乌斗牛怎么... 八分钟了解!越乡游义乌斗牛怎么赢,传送屋潮汕激k辅助,2025教程(有挂神器)1)越乡游义乌斗牛怎么...
5分钟了解!途游有辅助挂是真的... 5分钟了解!途游有辅助挂是真的吗,大宝麻将辅牌器是什么,新2025教程(有挂技巧)1、用户打开应用后...
九分钟了解!蛮籽麻将重庆有程序... 九分钟了解!蛮籽麻将重庆有程序吗,牵手跑胡子辅助器最新版,细节方法(有挂工具);1、下载好牵手跑胡子...