Android重写HorizontalScrollView只接收水平方向上滑动的事件

一、概述

最近在开发中遇到HorizontalScrollView嵌套XRecyclerView导致下拉刷新卡的情况,急横向水平滑动的View和下拉刷新的View,都在接收处理滑动事件,但是这种情况下触摸事件就会发生冲突。导致下拉刷新非常卡,这种情况下我们需要重写HorizontalScrollView只接收水平方向上滑动的事件。

二、重写HorizontalScrollView

package com.aldx.hccraftsman.view;



import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.HorizontalScrollView;

/**
 * 重写水平滑动View只接收水平方向上滑动的事件
 */
public class CustomHScrollView extends HorizontalScrollView{
     private GestureDetector mGestureDetector; 


        /**
         * @function CustomHScrollView constructor
         * @param context  Interface to global information about an application environment. 
         *                  
         */
        public CustomHScrollView(Context context) {
                super(context);
                // TODO Auto-generated constructor stub
                mGestureDetector = new GestureDetector(new HScrollDetector()); 
                setFadingEdgeLength(0); 
        }


        /**
         * @function CustomHScrollView constructor  
         * @param context Interface to global information about an application environment. 
         * @param attrs A collection of attributes, as found associated with a tag in an XML document.
         */
        public CustomHScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
            mGestureDetector = new GestureDetector(new HScrollDetector()); 
            setFadingEdgeLength(0); 
        }

        /**
         * @function  CustomHScrollView constructor  
         * @param context Interface to global information about an application environment. 
         * @param attrs A collection of attributes, as found associated with a tag in an XML document.
         * @param defStyle style of view
         */
        public CustomHScrollView(Context context, AttributeSet attrs,
                        int defStyle) {
                super(context, attrs, defStyle);
                // TODO Auto-generated constructor stub
                mGestureDetector = new GestureDetector(new HScrollDetector()); 
                setFadingEdgeLength(0); 
        }

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

        // Return false if we're scrolling in the y direction   
     class HScrollDetector extends SimpleOnGestureListener { 
            @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {        
             if(Math.abs(distanceX) > Math.abs(distanceY)) { 
                    return true; 
             } 

             return false; 
        } 
     } 

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值