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

相关内容

热门资讯

透视教学!wpk作弊是真的吗,... 透视教学!wpk作弊是真的吗,wpk是真的还是假的,必备教程(有挂解说)1、玩家可以在wpk是真的还...
透视ai代打!hhpoker视... 透视ai代打!hhpoker视频巡查真的假的,hhpoker到底可以作弊码,解说技巧(有挂解说)1、...
透视脚本!wpk有作弊吗,wp... 透视脚本!wpk有作弊吗,wpk模拟器,2025新版总结(有挂插件)1)wpk有作弊吗辅助挂:进一步...
透视肯定!hhpoker透视脚... 透视肯定!hhpoker透视脚本视频,hhpoker外挂靠谱吗,透视教程(有挂详情)1、hhpoke...
透视美元局!wpk刷入池率脚本... 透视美元局!wpk刷入池率脚本,wpk是真的还是假的,黑科技教程(有挂技巧);1、下载好wpk是真的...
透视透视!hhpoker怎么破... 透视透视!hhpoker怎么破解,hhpoker是内部控制吗,揭秘教程(有挂黑科技)1、每一步都需要...
透视脚本!wpk透视工作室,w... 透视脚本!wpk透视工作室,wpk辅助器是真的吗,攻略教程(有挂方法);1、点击下载安装,wpk辅助...
透视了解!hhpoker软件可... 透视了解!hhpoker软件可以玩吗,hhpoker有没有辅助,高科技教程(有挂细节)1)hhpok...
透视黑科技!wpk有辅助器吗,... 透视黑科技!wpk有辅助器吗,wpk作弊最怕三个东西,2025版教程(有挂解说)1、wpk作弊最怕三...
透视安卓版!hhpoker透视... 透视安卓版!hhpoker透视脚本下载,hhpoker辅助挂,科技教程(有挂揭秘);1、起透看视 h...