解決ScrollView嵌套RecyclerView時滑動沖突:

前言:

在實際的項目開發(fā)中,項目需求所需,需要將ScrollView和RecyclerView嵌套使用,在使用過程中發(fā)現(xiàn)兩者配合使用存在滑動沖突問題,現(xiàn)將解決辦法記錄在冊,以便后續(xù)查看。話不多少,直接上,能動手絕不瞎嗶嗶。

1.最暴力的辦法就是直接禁止recyclerView滑動;

GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2) {
       
    @Override
    public boolean canScrollVertically() {
        return false;
    }
};

2. 重寫LayoutManager,重寫canScrollVertically()這個方法。

public class ScrollGridLayoutManagerw extends GridLayoutManager {
    //是否支持滑動
    private boolean isScrollEnabled = true;

    public ScrollGridLayoutManagerw(Context context, int spanCount) {
        super(context, spanCount);
    }

    public ScrollGridLayoutManagerw(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

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

    /**
     * 是否支持滑動
     *
     * @param flag
     */
    public void setScrollEnabled(boolean flag) {
        this.isScrollEnabled = flag;
    }

    @Override
    public boolean canScrollVertically() {

        //isScrollEnabled:recyclerview 是否支持滑動
        //super.canScrollVertically():是否為豎直方向滾動
    return isScrollEnabled && super.canScrollVertically();
    }
}

使用的時候在代碼中設置:

LayoutManager.setScrollEnabled(false);

3. 新建一個類繼承LayoutManager,通過重寫onMeasure()方法重新測量RecyclerView寬高,在代碼中設置RecyclerView.setNestedScrollingEnabled(false)來解決。

public class ScrollGridLayoutManager extends GridLayoutManager {
    private static final String TAG = ScrollGridLayoutManager.class.getSimpleName();

    public ScrollGridLayoutManager(Context context, int spanCount) {
        super(context, spanCount);
    }

    private int[] mMeasuredDimension = new int[2];

    @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);

        Log.i(TAG, "onMeasure called. \nwidthMode " + widthMode
                + " \nheightMode " + heightSpec
                + " \nwidthSize " + widthSize
                + " \nheightSize " + heightSize
                + " \ngetItemCount() " + getItemCount());

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

            if (getOrientation() == HORIZONTAL) {
                width = width + mMeasuredDimension[0];
                if (i == 0) {
                    height = mMeasuredDimension[1];
                }
            } else {
                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) {
        try {
            View view = recycler.getViewForPosition(0);//fix 動態(tài)添加時報IndexOutOfBoundsException

            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);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }
}

使用的時候在代碼中設置:

ScrollGridLayoutManager gridLayoutManager = new ScrollGridLayoutManager(getContext(),2);
recyclerView.setNestedScrollingEnabled(false);

通過上述三種方法就可以解決ScrollView嵌套RecyclerView時的滑動沖突。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容