这两天写了一个拖动控件,需要集成Linearlayout,重写了其中的onInterceptTouchEvent事件,其中遇到一个问题,MotionEvent.ACTION_DOWN中return false时拖动控件总会引起button的点击效果(只是效果,不是点击事件),改为return true的话Button又无法引起点击事件,肯定是不行的。
后来看了下源代码,发现button点击时MotionEvent.ACTION_DOWN事件中会判断当前父控件是否为滚动控件,是的话会延迟触发点击效果,以下是源代码
if (isInScrollingContainer) {
mPrivateFlags |= PFLAG_PREPRESSED;
if (mPendingCheckForTap == null) {
mPendingCheckForTap = new CheckForTap();
}
postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
} else {
// Not inside a scrolling container, so show the feedback right away
setPressed(true);
checkForLongClick(0);
}
而Linearlayout默认重写isInScrollingContainer方法return false,由此可知,如果想要在拖动时不触发button的点击效果需要重写isInScrollingContainer方法return true。