美文网首页
RecyclerView嵌套RecyclerView画廊

RecyclerView嵌套RecyclerView画廊

作者: OK2018 | 来源:发表于2017-12-14 16:59 被阅读0次
    
    
    /**
     * Created by Liu on 2017/8/3.
    * 里层的RecyclerView
     * 解决ScrollView与RecyclerView横向滚动时的事件冲突
     */
    public class ScrollRecyclerView extends RecyclerView {
    
        public ScrollRecyclerView(Context context) {
            super(context);
        }
    
        public ScrollRecyclerView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        public ScrollRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        private float lastX, lastY;
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent e) {
    
            boolean intercept = super.onInterceptTouchEvent(e);
    
            switch (e.getAction()) {
    
                case MotionEvent.ACTION_DOWN:
                    lastX = e.getX();
                    lastY = e.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    // 只要横向大于竖向,就拦截掉事件。
                    float slopX = Math.abs(e.getX() - lastX);
                    float slopY = Math.abs(e.getY() - lastY);
                    //  Log.log("slopX=" + slopX + ", slopY="  + slopY);
                    // 此处不可以等于 if( slopX >= slopY){
                    if( slopX > slopY){
                        requestDisallowInterceptTouchEvent(true);
                        intercept = true;
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    intercept = false;
                    break;
            }
            // Log.log("intercept"+e.getAction()+"=" + intercept);
            return intercept;
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:RecyclerView嵌套RecyclerView画廊

          本文链接:https://www.haomeiwen.com/subject/hlgkwxtx.html