SwipeMenuListView在ScrollView里上下滑动导致菜单不能显示完全的bug解决方法

这是因为上下滑动的时候,事件被ScrollView截获了,这时候应该禁止ScrollView截获上下滑动事件,解决方法如下

public class NoRollSwipeMenuListView extends SwipeMenuListView {

    private GestureDetector mGestureDetector;

    public NoRollSwipeMenuListView(Context context) {
        super(context);
        mGestureDetector = new GestureDetector(context, onGestureListener);
    }

    public NoRollSwipeMenuListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(context, onGestureListener);
    }

    public NoRollSwipeMenuListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mGestureDetector = new GestureDetector(context, onGestureListener);
    }

    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        boolean b = mGestureDetector.onTouchEvent(ev);
//        LogUtil.w("onTouchEvent", "mGestureDetector.onTouchEvent(ev)->" + b);
        return super.onTouchEvent(ev);
    }

    private GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener() {

        //distanceX 左右滑动距离,左滑动正值,右滑动负值
        //distanceY 上下滑动距离,上滑动正值,下滑动负值
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (Math.abs(distanceY) >= Math.abs(distanceX)) {//上下滑动距离大于左右滑动距离,当作上下滑动
//                LogUtil.w("onScroll", "distanceX=" + distanceX + ":distanceY=" + distanceY);
//                LogUtil.w("onScroll", "true");
                //上下滑动不做任何操作,在这里父ScrollView已经交出onTouch权限,否则如果权限在父ScrollView的话这里接收不到事件
                //所以执行到这里是因为下面的setParentScrollAble(false);已经执行过了
                return true;
            }
            //当滑动NoRollSwipeMenuListView的时候,让父ScrollView交出onTouch权限,也就是让父ScrollView停住不能滚动
            setParentScrollAble(false);
//            LogUtil.w("onScroll", "false");
            return false;
        }
    };

    /**
     * 是否把滚动事件交给父ScrollView
     *
     * @param flag
     */
    private void setParentScrollAble(boolean flag) {
        //这里的parentScrollView就是NoRollSwipeMenuListView外面的那个ScrollView
//        LogUtil.w("setParentScrollAble", "flag->" + flag);
        getParent().requestDisallowInterceptTouchEvent(!flag);
    }
}

 

转载于:https://www.cnblogs.com/supermanChao/p/5832577.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值