HorizontalScrollView中嵌套使用带侧拉删除的RecyclerView滑动冲突解决

最近在项目中使用了带侧拉删除的RecyclerView,由于RecyclerView横向数据较多,设计不可以换行,所以在外层使用了HorizontalScrollView,出现了RecyclerView的侧滑事件不响应。

解决思路:对HorizontalScrollView进行滑动监听,当向左活动并且滑动到尾部时,将滑动事件分发至子控件,否则将滑动事件拦截:

1、自定义HorizontalScrollView,实现对HorizontalScrollView滑动事件的监听以及滑动事件的分发

/**
 * Created by james on 2018-04-22.
 * 滚动状态监听
 */

public class MyHorizontalScrollView extends HorizontalScrollView implements ScrollViewTag {

    private ScrollViewListener scrollViewListener = null;
    private float startX;
    private float startY;
    private float endX;
    private float endY;
    private boolean TAG = false;

    public MyHorizontalScrollView(Context context) {
        super(context);
    }

    public MyHorizontalScrollView(Context context, AttributeSet attrs,
                                  int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return super.onTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = (int) ev.getRawX();
                startY = (int) ev.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                endX = (int) ev.getRawX();
                endY = (int) ev.getRawY();
                if (Math.abs(endX - startX) > Math.abs(endY - startY)) {// 左右滑动
                    if (endX < startX && TAG)//左滑
                        //把事件传递到子view中
                        return false;
                    else
                        return super.onInterceptTouchEvent(ev);
                } else {
                    return super.onInterceptTouchEvent(ev);
                }
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public void getTag(boolean is) {
        TAG = is;
    }
}

2、ScrollViewTag

public interface ScrollViewTag {
    void getTag(boolean is);
}

3、ScrollViewListener

public interface ScrollViewListener {
    void onScrollChanged(MyHorizontalScrollView scrollView, int x, int y, int oldx, int oldy);
}

4、在项目中使用

 horizontalScrollView.setScrollViewListener(new ScrollViewListener() {
            @Override
            public void onScrollChanged(MyHorizontalScrollView scrollView, int x, int y, int oldx, int oldy) {
                int scrollX = scrollView.getScrollX();
                int width = scrollView.getWidth();
                int scrollViewMeasuredWidth = title_layout.getMeasuredWidth();
                if ((scrollX + width) == scrollViewMeasuredWidth) {
//                    showToast("滑动到了底部");
                    horizontalScrollView.getTag(true);
                } else {
                    horizontalScrollView.getTag(false);
                }
            }
        });
title_layout.getMeasuredWidth() :测量得到recycleView显示的宽度

你可能感兴趣的:(HorizontalScrollView中嵌套使用带侧拉删除的RecyclerView滑动冲突解决)