HorizontalScrollView嵌套RecyclerView冲突实现滑动删除

    最近碰到一个控件冲突的问题,参考网址:https://blog.csdn.net/qq_37011271/article/details/80039837

解决思路:对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、

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

3、

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

 RecycleView的侧滑代码参考网址:

你可能感兴趣的:(控件)