问题描述
为什么ViewGroup中唯一获得ACTION_DOWN在onInterceptTouchEvent?根据文档,只要返回false应接收所有的事件类型。http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent%28android.view.MotionEvent%29点#3。
样品code:
公共类MainActivity延伸活动{
私有静态最后字符串变量= MainActivity.class.getSimpleName();
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(新容器(本));
}
私有类容器扩展的LinearLayout {
公共集装箱(上下文的背景下){
超(上下文);
setBackgroundColor(0xFF0000FF);
}
@覆盖
公共布尔onInterceptTouchEvent(MotionEvent EV){
Log.i(TAG,onInterceptTouchEvent);
INT行动= ev.getActionMasked();
开关(动作){
案例MotionEvent.ACTION_DOWN:
Log.i(TAG,onInterceptTouchEvent.ACTION_DOWN);
打破;
案例MotionEvent.ACTION_MOVE:
Log.i(TAG,onInterceptTouchEvent.ACTION_MOVE);
打破;
案例MotionEvent.ACTION_CANCEL:
案例MotionEvent.ACTION_UP:
Log.i(TAG,onInterceptTouchEvent.ACTION_UP);
打破;
}
返回super.onInterceptTouchEvent(EV);
}
}
}
我会回答我的问题:onInterceptTouchEvent只被调用,如果父母有从的onTouchEvent返回true子视图。一旦孩子返回true,家长现在有机会截获该事件。
Why do ViewGroup's only get ACTION_DOWN in the onInterceptTouchEvent? According to the docs, as long as false is returned it should receive all the event types.http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent%28android.view.MotionEvent%29Point #3.
sample code:
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new Container(this));
}
private class Container extends LinearLayout {
public Container(Context context) {
super(context);
setBackgroundColor(0xFF0000FF);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.i(TAG, "onInterceptTouchEvent");
int action = ev.getActionMasked();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.i(TAG, "onInterceptTouchEvent.ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.i(TAG, "onInterceptTouchEvent.ACTION_MOVE");
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
Log.i(TAG, "onInterceptTouchEvent.ACTION_UP");
break;
}
return super.onInterceptTouchEvent(ev);
}
}
}
I'll answer my own question: onInterceptTouchEvent only get called if the parent has a child view which returns "true" from onTouchEvent. Once the child returns true, the parent now has a chance to intercept that event.
这篇关于onInterceptTouchEvent只得到ACTION_DOWN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!