Android CoordinatorLayout和Behavior的源码分析(四)

三、Behavior拦截机制


右滑删除view的实现(SwipeDismissBehavior),是通过重写Behavior的下面的方法实现:

 @Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    switch (MotionEventCompat.getActionMasked(event)) {
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            // Reset the ignore flag
            if (mIgnoreEvents) {
                mIgnoreEvents = false;
                return false;
            }
            break;
        default:
            mIgnoreEvents = !parent.isPointInChildBounds(child,
                    (int) event.getX(), (int) event.getY());
            break;
    }

    if (mIgnoreEvents) {
        return false;
    }

    ensureViewDragHelper(parent);
    return mViewDragHelper.shouldInterceptTouchEvent(event);
}

@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (mViewDragHelper != null) {
        mViewDragHelper.processTouchEvent(event);
        return true;
    }
    return false;
}

前面说过Behavior相当于一个代理,那Behavior如何实现代理功能的呢?

   CoordinatorLayout.onInterceptTouchEvent
CoordinatorLayout.onTouchEvent
     CoordinatorLayout.performIntercept
             MyBehavior.onInterceptTouchEvent
             MyBehavior.onTouchEvent

在Col中onInterceptTouchEvent和onTouchEvent方法中调用performIntercept方法看子view的Behavoir是否拦截:

  private boolean performIntercept(MotionEvent ev, final int type) {
    ...
    for (int i = 0; i < childCount; i++) {
        final View child = topmostChildList.get(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        final Behavior b = lp.getBehavior();
        ...
        if (!intercepted && b != null) {
            switch (type) {
                case TYPE_ON_INTERCEPT:
                    intercepted = b.onInterceptTouchEvent(this, child, ev);
                    break;
                case TYPE_ON_TOUCH:
                    intercepted = b.onTouchEvent(this, child, ev);
                    break;
            }
            if (intercepted) {
                mBehaviorTouchView = child;
            }
        }
    ...
    return intercepted;
}

在这个方法中会循环遍历所有的子view,看看谁的代理Behavior要拦截事件。

参考资料

  1. 官网: https://developer.android.com/reference/android/support/design/widget/CoordinatorLayout.html
  2. Behavior的实现原理:http://www.myexception.cn/mobile/2052223.html
  3. 嵌套滑动机制:http://blog.csdn.net/wuyuxing24/article/details/51112645
  4. Behavior的拦截一切的机制:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2016/0224/3991.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值