为什么ViewGroup唯一得到ACTION_DOWN的onInterceptTouchEvent?根据文档,只要返回false,它就应该接收所有事件类型.
http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent%28android.view.MotionEvent%29
第3点.
示例代码:
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 …Run Code Online (Sandbox Code Playgroud) 如何找到导致MotionEvent ACTION_CANCEL的视图?我有一个接收ACTION_CANCEL的视图"A",我不希望这种情况发生.在某处,视图"B"正在"消耗"MotionEvent.我希望有办法找出谁是"B",这样我才能解决这个错误的问题.
我已经尝试查看我的代码中的各种OnTouchEvent()和OnInterceptTouchEvent()处理程序,但还没有找到罪魁祸首.
我还在有问题的ACTION_CANCEL上设置了一个断点,但是我无法识别可能代表"B"的MotionEvent中的任何内容.
在我的布局中,我有一个像这样的结构:
--RelativeLayout
|
--FrameLayout
|
--Button, EditText...
Run Code Online (Sandbox Code Playgroud)
我想在RelativeLayout和FrameLayout中处理触摸事件,所以我在这两个视图组中设置了onTouchListener.但只捕获RelativeLayout中的触摸.
为了尝试解决这个问题,我编写了自己的CustomRelativeLayout,并覆盖了onInterceptTouchEvent,现在捕获了子ViewGroup(FrameLayout)中的点击,但按钮和其他视图中的单击没有任何效果.
在我自己的自定义布局中,我有这个:
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
Run Code Online (Sandbox Code Playgroud)