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有猫腻!aapo... aapoker有猫腻!aapoker俱乐部后天可以操作吗,(aapoker安卓)原来是真的有挂(详细...
微扑克德州专用辅助器!微扑克怎... 大家肯定在之前微扑克德州专用辅助器或者微扑克德州专用辅助器中玩过微扑克德州专用辅助器!微扑克怎么用a...
wepokeai代打的胜率!w... wepokeai代打的胜率!wepoke靠谱吗(wepoke德州扑克)一贯是有挂(详细ai辅助教程)...
红龙扑克辅助!红龙扑克都是机器... 红龙扑克辅助!红龙扑克都是机器人吗,(红龙扑克)果然真的有挂(详细辅助教程);简单的灵活控制,仅凭一...
wepoke辅助德之星!wep... wepoke辅助德之星!wepoke软件还可以用吗(Wepoke安卓)竟然是真的有挂(详细辅助技巧教...
wepoke模拟器!wepok... 1、wepoke模拟器!wepoke软件透明(Wepokeplus)总是存在有挂(详细辅助教程)。2...
wepoke ai辅助!wep... wepoke ai辅助!wepoke软件收费吗(Wepoke针对)好像真的有挂(详细有挂教程);we...
红龙扑克辅助工具!红龙扑克有没... 1、红龙扑克辅助工具!红龙扑克有没有外卦挂,(红龙扑克)确实是真的有挂(详细辅助挂教程)。2、透视辅...
wepoke智能ai!wepo... 此外,数据分析德州(wepoke智能ai)辅助神器app还具备辅助透视行为开挂功能,通过对客户wep...
智星德州菠萝辅助器!德扑助手,... 智星德州菠萝辅助器!德扑助手,德扑ai软件总是是真的有挂(详细房间设置教程)是一款可以让一直输的玩家...