ViewGroup
View
Activity dispatchTouchEvent
有
有
有 onInterceptTouchEvent
有
无
无 onTouchEvent
有
有
有
例子零: 只有OnTouch和OnItemClick
actualListView.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(MainActivity.this, "DOWN",
Toast.LENGTH_SHORT).show();
return false; //这儿是true,下面是false,onItemClick会走 case MotionEvent.ACTION_UP:
Toast.makeText(MainActivity.this, "UP",
Toast.LENGTH_SHORT).show();
return true; //上面随意,只要这个地方返回true,onItemClick就不会走
}
return false;
}
});
actualListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "setOnClickListener",
Toast.LENGTH_SHORT).show();
} });
例子一: 只有OnTouch和OnItemClick
actualListView.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(MainActivity.this, "DOWN",
Toast.LENGTH_SHORT).show();
break; case MotionEvent.ACTION_UP:
Toast.makeText(MainActivity.this, "UP",
Toast.LENGTH_SHORT).show();
break;
}
return false; //返回false之后
}
});
actualListView.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "setOnClickListener", Toast.LENGTH_SHORT).show();
}
});
先走ACTION_DOWN,ACTION_UP,最后走onItemClick。
例子二:只有OnTouch和OnItemClick
actualListView.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(MainActivity.this, "DOWN",
Toast.LENGTH_SHORT).show();
break; case MotionEvent.ACTION_UP:
Toast.makeText(MainActivity.this, "UP",
Toast.LENGTH_SHORT).show();
break;
}
return true; //返回true之后
}
});
actualListView.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "setOnClickListener", Toast.LENGTH_SHORT).show();
}
});
先走ACTION_DOWN,ACTION_UP, onItemClick不走
例子三:只有OnTouch和OnItemClick和dispatchTouchEvent
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(MainActivity.this, "dispatch DOWN",
Toast.LENGTH_SHORT).show();
break; case MotionEvent.ACTION_UP:
Toast.makeText(MainActivity.this, "dispatch UP",
Toast.LENGTH_SHORT).show();
break;
} return super.dispatchTouchEvent(ev);
}
actualListView.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(MainActivity.this, "DOWN",
Toast.LENGTH_SHORT).show();
break; case MotionEvent.ACTION_UP:
Toast.makeText(MainActivity.this, "UP",
Toast.LENGTH_SHORT).show();
break;
}
return false; //返回false之后
}
});
actualListView.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "setOnClickListener", Toast.LENGTH_SHORT).show();
}
});
先走dispatchTouchEvent的ACTION_DOWN------------>OnTouch的ACTION_DOWN------------->dispatchTouchEvent的ACTION_UP
----------------->OnTouch的ACTION_UP----------------->onItemClick
例子四:只有OnTouch和OnItemClick和dispatchTouchEvent
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(MainActivity.this, "dispatch DOWN",
Toast.LENGTH_SHORT).show();
break; case MotionEvent.ACTION_UP:
Toast.makeText(MainActivity.this, "dispatch UP",
Toast.LENGTH_SHORT).show();
break;
} return super.dispatchTouchEvent(ev);
}
actualListView.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(MainActivity.this, "DOWN",
Toast.LENGTH_SHORT).show();
break; case MotionEvent.ACTION_UP:
Toast.makeText(MainActivity.this, "UP",
Toast.LENGTH_SHORT).show();
break;
}
return true; //返回true之后
}
});
actualListView.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "setOnClickListener", Toast.LENGTH_SHORT).show();
}
});
先走dispatchTouchEvent的ACTION_DOWN------------>OnTouch的ACTION_DOWN------------->dispatchTouchEvent的ACTION_UP
----------------->OnTouch的ACTION_UP
例子五:只有OnTouch和OnItemClick和dispatchTouchEvent
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(MainActivity.this, "dispatch DOWN",
Toast.LENGTH_SHORT).show();
return true; //这儿无论是true还是false,都会导致OnTouch和OnItemClick不走,因为没有下面的super来分发事件在activity中的操作 case MotionEvent.ACTION_UP:
Toast.makeText(MainActivity.this, "dispatch UP",
Toast.LENGTH_SHORT).show();
return true; //这儿无论是true还是false,都会导致OnTouch和OnItemClick不走,因为没有下面的super来分发事件在activity中的操作
}
return super.dispatchTouchEvent(ev);
}
actualListView.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(MainActivity.this, "DOWN",
Toast.LENGTH_SHORT).show();
break; case MotionEvent.ACTION_UP:
Toast.makeText(MainActivity.this, "UP",
Toast.LENGTH_SHORT).show();
break;
}
return false; //返回false之后
}
});
actualListView.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "setOnClickListener", Toast.LENGTH_SHORT).show();
}
});
先走dispatchTouchEvent的ACTION_DOWN---------------------->dispatchTouchEvent的ACTION_UP
例子六:有OnTouch和OnItemClick 和 dispatchTouchEvent和onTouchEvent
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(MainActivity.this, "dispatch DOWN",
Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
Toast.makeText(MainActivity.this, "dispatch UP",
Toast.LENGTH_SHORT).show();
break;
} return super.dispatchTouchEvent(ev);
} @Override
public boolean onTouchEvent(MotionEvent event) {
Toast.makeText(MainActivity.this, "EveUP",
Toast.LENGTH_SHORT).show();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(MainActivity.this, "Event DOWN",
Toast.LENGTH_SHORT).show();
return true;
case MotionEvent.ACTION_UP:
Toast.makeText(MainActivity.this, "Event UP",
Toast.LENGTH_SHORT).show();
return true;
}
return super.onTouchEvent(event);
} Button button = (Button) findViewById(R.id.go_shopping);//这儿改为CLICKABLE的imageview结果一样
button.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(MainActivity.this, "DOWN",
Toast.LENGTH_SHORT).show();
return false; //这儿是true,下面是false,onItemClick会走 case MotionEvent.ACTION_UP:
Toast.makeText(MainActivity.this, "UP",
Toast.LENGTH_SHORT).show();
return false; //上面随意,只要这个地方返回true,onItemClick就不会走
}
return false;
}
});
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "setOnClickListener",
Toast.LENGTH_SHORT).show();
}
});
先走dispatchTouchEvent的ACTION_DOWN------------>OnTouch的ACTION_DOWN------------->dispatchTouchEvent的ACTION_UP
----------------->OnTouch的ACTION_UP------------>OnClickListener
activity的伪生命周期onTouchEvent没有走,是因为button的onTouchEvent把事件消费了,
例子七:有OnTouch和OnItemClick 和 dispatchTouchEvent和onTouchEvent
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(MainActivity.this, "dispatch DOWN",
Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
Toast.makeText(MainActivity.this, "dispatch UP",
Toast.LENGTH_SHORT).show();
break;
} return super.dispatchTouchEvent(ev);
} @Override
public boolean onTouchEvent(MotionEvent event) {
Toast.makeText(MainActivity.this, "EveUP",
Toast.LENGTH_SHORT).show();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(MainActivity.this, "Event DOWN",
Toast.LENGTH_SHORT).show();
return true;
case MotionEvent.ACTION_UP:
Toast.makeText(MainActivity.this, "Event UP",
Toast.LENGTH_SHORT).show();
return true;
}
return super.onTouchEvent(event);
} Button button = (Button) findViewById(R.id.go_shopping); //这儿改为imageview结果一样
button.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(MainActivity.this, "DOWN",
Toast.LENGTH_SHORT).show();
return false; //这儿是true,下面是false,onItemClick会走 case MotionEvent.ACTION_UP:
Toast.makeText(MainActivity.this, "UP",
Toast.LENGTH_SHORT).show();
return true; //上面随意,只要这个地方返回true,onItemClick就不会走
}
return false;
}
});
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "setOnClickListener",
Toast.LENGTH_SHORT).show();
}
});
先走dispatchTouchEvent的ACTION_DOWN------------>OnTouch的ACTION_DOWN------------->dispatchTouchEvent的ACTION_UP
----------------->OnTouch的ACTION_UP
button的OnClickListener没走,是因为onTouch 在ACTION_UP中返回true,把事件给消费了,导致button的onTouchEvent没走,连带的button的OnClick
也没有走。
activity的伪生命周期onTouchEvent没有走,是因为button的onTouch把事件消费了。
例子八:有 dispatchTouchEvent和onTouchEvent
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(MainActivity.this, "dispatch DOWN",
Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
Toast.makeText(MainActivity.this, "dispatch UP",
Toast.LENGTH_SHORT).show();
break;
} return super.dispatchTouchEvent(ev);
} @Override
public boolean onTouchEvent(MotionEvent event) {
Toast.makeText(MainActivity.this, "EveUP",
Toast.LENGTH_SHORT).show();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(MainActivity.this, "Event DOWN",
Toast.LENGTH_SHORT).show();
return true;
case MotionEvent.ACTION_UP:
Toast.makeText(MainActivity.this, "Event UP",
Toast.LENGTH_SHORT).show();
return true;
}
return super.onTouchEvent(event);
} Button button = (Button) findViewById(R.id.go_shopping);
//不给button设置ontouch和onclick
先走dispatchTouchEvent的ACTION_DOWN----------------->dispatchTouchEvent的ACTION_UP
activity的伪生命周期onTouchEvent没有走,是因为button的dispatchTouchEvent------>onTouchEvent把事件消费了。即使没有ontouch,事件也会被
button给消费掉
viewgroup(listview)和view(button)一样,其onTouchEvent会把事件给消费了,和view(imageview)不一样。
要想activity的伪生命周期onTouchEvent可以走的话,可以把button改为imageview,因为imageview不是“可按的CLICKABLE”,所以会imageview的onTouchEvent会return个false,不会消费掉这个事件
会先走dispatchTouchEvent的ACTION_DOWN------------>onTouchEvent的ACTION_DOWN------------->dispatchTouchEvent的ACTION_UP
----------------->onTouchEvent的ACTION_UP
点击一个ViewGrounp中的Button,事件的传递
Activity的dispatchTouchEvent(返回true或false都会导致事件不会分发,要返回super)----->ViewGrounp的dispatchTouchEvent-------->ViewGrounp的onInterceptTouchEvent()(返回false不拦截)--------->Button的dispatchTouchEvent------------->Button的setOnTouchListener(在up/down时如果返回false)------------->Button的onTouchEvent默认返回true消费事件(imageview默认返回false,事件会向上传/第二个事件的传递看我的另一个文章Android Touch事件传递机制 二)------>Button的setOnClickListener(在onTouchEvent中的up时调用)
Activity的dispatchTouchEvent(返回true或false都会导致事件不会分发,要返回super)----->ViewGrounp的dispatchTouchEvent-------->ViewGrounp的onInterceptTouchEvent()(返回false不拦截)--------->Button的dispatchTouchEvent------------->Button的setOnTouchListener(在up/down时如果返回true)消费事件结束
Activity的dispatchTouchEvent(返回true或false都会导致事件不会分发,要返回super)----->ViewGrounp的dispatchTouchEvent-------->ViewGrounp的onInterceptTouchEvent()(返回false不拦截)--------->Button的dispatchTouchEvent(Button的setOnTouchListener为null)------------->Button的onTouchEvent默认返回true----------->Button的setOnClickListener(在onTouchEvent中的up时调用)