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;
    }
}

相关内容

热门资讯

透视挂!aapoker公共底牌... 透视挂!aapoker公共底牌(透视)插件下载(原来是真的有挂);1、上手简单,内置详细流程视频教学...
透视好友!pokermaste... 透视好友!pokermaster脚本,智星菠萝有挂吗(确实存在有挂)1、完成智星菠萝有挂吗的残局,帮...
透视教程!菠萝德州透视脚本,p... 透视教程!菠萝德州透视脚本,pokemmo辅助器手机版下载(都是是真的有挂);1、pokemmo辅助...
透视脚本!aapoker俱乐部... 透视脚本!aapoker俱乐部靠谱吗(透视)透视脚本(总是有挂)1、每一步都需要思考,不同水平的挑战...
透视辅助!拱趴游戏破解器,哈糖... 透视辅助!拱趴游戏破解器,哈糖大菠萝可以开挂吗(真是存在有挂)1、哈糖大菠萝可以开挂吗机器人多个强度...
透视苹果版!aapoker发牌... 透视苹果版!aapoker发牌逻辑(透视)真的假的(竟然是真的有挂)1、首先打开aapoker发牌逻...
透视玄学!约局吧透视挂下载,约... 透视玄学!约局吧透视挂下载,约局吧辅助器(原来真的有挂);小薇(透视辅助)致您一封信;亲爱约局吧透视...
透视ai代打!aapoker ... 透视ai代打!aapoker ai插件(透视)万能辅助器(切实真的是有挂)透视ai代打!aapoke...
透视系统!拱趴大菠萝机器人,哈... 透视系统!拱趴大菠萝机器人,哈糖大菠萝挂(原来有挂)拱趴大菠萝机器人辅助器中分为三种模型:拱趴大菠萝...
透视脚本!aa poker辅助... 透视脚本!aa poker辅助(透视)透视脚本下载(本来是真的有挂)1、下载好aa poker辅助辅...