Android 重写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 自己消费,false 父类消费
                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);
                //滑动的角度
                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;//滑动角度是否大于45du
                boolean isMeetSlidingXAngle = xAngle > SLIDE_ANGLE;//滑动角度是否大于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, "向上滑动");
                    // 是否滑动到底部了
                    boolean isBottom = !canScrollVertically(DOWN);
                    Log.d(TAG, "isBottom:" + isBottom);
                    // 父类不拦截,自己消费  没用滑动到底部,自己消费
                    getParent().requestDisallowInterceptTouchEvent(!isBottom);
                } else if (isSlideDown) {
                    Log.d(TAG, "向下滑动");
                    // 是否到顶了
                    boolean isTop = !canScrollVertically(UP);
                    Log.d(TAG, "isTop:" + isTop);
                    if (isTop) {
                        if (getParent() instanceof SwipeRefreshLayout) {
                            SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) getParent();
                            boolean hasLoadMore = swipeRefreshLayout.isEnabled();
                            // 能加载更多,父类不拦截,自己消费
                            getParent().requestDisallowInterceptTouchEvent(hasLoadMore);
                        } else {
                            getParent().requestDisallowInterceptTouchEvent(false);
                        }
                    } else {
                        getParent().requestDisallowInterceptTouchEvent(true);
                    }
                } else if (isSlideLeft) {
                    Log.d(TAG, "向左边滑动");
                    getParent().requestDisallowInterceptTouchEvent(false);
                } else if (isSlideRight) {
                    getParent().requestDisallowInterceptTouchEvent(false);
                    Log.d(TAG, "向右边滑动");
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                getParent().requestDisallowInterceptTouchEvent(false);
                break;
            default:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容