Android中的事件冲突ScrollView嵌套ListView-----带你轻松入门

在垒代码时总会遇到控件嵌套控件的这种问题,同时也会遇到一些冲突,其中最经典的就是ScrollView嵌套ListView,虽然网上也是提供了大量的解决方案,这些方案在给自己解决问题的同时也带来了宝贵的思路.

废话不多说先给大家来一张图让大家看一下. 

这是自己写的一个Demo是有事件冲突的样子

趁着看图的时间大家先思考一个问题

问:ScrollView嵌套ListView为什么会产生冲突

我们先来看看ScrollView的onInterceptTouchEvent(..)

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*
     * This method JUST determines whether we want to intercept the motion.
     * If we return true, onMotionEvent will be called and we do the actual
     * scrolling there.
     */

    /*
    * Shortcut the most recurring case: the user is in the dragging
    * state and he is moving his finger.  We want to intercept this
    * motion.
    */
    final int action = ev.getAction();
     //这个滑动事件没有问题                mIsBeingDragged这个???? 接下来我们看看是什么意思
    if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged)) {
        return true;
    }
//下面的代码忽略不计
}



    /**
     * True if the user is currently dragging this ScrollView around. This is
     * 如果用户当前正在拖动滚动视图,则为True
     * not the same as 'is being flinged', which can be checked by
     * mScroller.isFinished() (flinging begins when the user lifts his finger).
     */
    private boolean mIsBeingDragged = false;

这个mIsBeingDragged是最小滑动像素,只有超过这个值才能滑动,所以在我们滑动的那一瞬间就被拦截,所以Listview不能滑动,

仔细观察我们会发现在MotionEvent.ACTION_MOVE发生前就会产生一个MotionEvent.ACTION_DOWN,所以说在mIsBeingDragged之前事件是没有被拦截的,

既然MotionEvent.ACTION_MOVE没有被拦截我们可以在ListView中接收MotionEvent.ACTION_MOVE和MotionEvent.ACTION_DOWN事件,然后顺着这个思路进行请求ScrollView的拦截事件

接下来看修改后的---------

不足之处请大家点评.....

欢迎访问GitHub:https://github.com/BeaHugs/ScrollViewNestListView

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值