Android 重寫(xiě)recycleView解決與豎版viewpager沖突


public class TopFadingEdgeRecyclerView extends RecyclerView {
    public static final String TAG = "chenhaocc";
    private float downX, downY;
    private static final float SLIDE_ANGLE = 45;
    private static final int UP = -1;
    private static final int DOWN = 1;

    public TopFadingEdgeRecyclerView(@NonNull @NotNull Context context) {
        super(context);
    }

    public TopFadingEdgeRecyclerView(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TopFadingEdgeRecyclerView(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected float getTopFadingEdgeStrength() {
        return 0.5f;
    }

    @Override
    protected float getBottomFadingEdgeStrength() {
        return 0;
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = ev.getX();
                downY = ev.getY();
                // requestDisallowInterceptTouchEvent  true 自己消費(fèi),false 父類(lèi)消費(fèi)
                getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_MOVE:
                float moveX = ev.getX();
                float moveY = ev.getY();
                float xDiff = Math.abs(moveX - downX);
                float yDiff = Math.abs(moveY - downY);
                double squareRoot = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
                //滑動(dòng)的角度
                int yAngle = Math.round((float) (Math.asin(yDiff / squareRoot) / Math.PI * 180));
                int xAngle = Math.round((float) (Math.asin(xDiff / squareRoot) / Math.PI * 180));
                boolean isMeetSlidingYAngle = yAngle > SLIDE_ANGLE;//滑動(dòng)角度是否大于45du
                boolean isMeetSlidingXAngle = xAngle > SLIDE_ANGLE;//滑動(dòng)角度是否大于45du
                boolean isSlideUp = moveY < downY && isMeetSlidingYAngle;
                boolean isSlideDown = moveY > downY && isMeetSlidingYAngle;
                boolean isSlideLeft = moveX < downX && isMeetSlidingXAngle;
                boolean isSlideRight = moveX > downX && isMeetSlidingXAngle;
                if (isSlideUp) {
                    Log.d(TAG, "向上滑動(dòng)");
                    // 是否滑動(dòng)到底部了
                    boolean isBottom = !canScrollVertically(DOWN);
                    Log.d(TAG, "isBottom:" + isBottom);
                    // 父類(lèi)不攔截,自己消費(fèi)  沒(méi)用滑動(dòng)到底部,自己消費(fèi)
                    getParent().requestDisallowInterceptTouchEvent(!isBottom);
                } else if (isSlideDown) {
                    Log.d(TAG, "向下滑動(dòng)");
                    // 是否到頂了
                    boolean isTop = !canScrollVertically(UP);
                    Log.d(TAG, "isTop:" + isTop);
                    if (isTop) {
                        if (getParent() instanceof SwipeRefreshLayout) {
                            SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) getParent();
                            boolean hasLoadMore = swipeRefreshLayout.isEnabled();
                            // 能加載更多,父類(lèi)不攔截,自己消費(fèi)
                            getParent().requestDisallowInterceptTouchEvent(hasLoadMore);
                        } else {
                            getParent().requestDisallowInterceptTouchEvent(false);
                        }
                    } else {
                        getParent().requestDisallowInterceptTouchEvent(true);
                    }
                } else if (isSlideLeft) {
                    Log.d(TAG, "向左邊滑動(dòng)");
                    getParent().requestDisallowInterceptTouchEvent(false);
                } else if (isSlideRight) {
                    getParent().requestDisallowInterceptTouchEvent(false);
                    Log.d(TAG, "向右邊滑動(dòng)");
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                getParent().requestDisallowInterceptTouchEvent(false);
                break;
            default:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容