編輯:關於android開發
例子零: 只有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來分發事件 case MotionEvent.ACTION_UP: Toast.makeText(MainActivity.this, "dispatch UP", Toast.LENGTH_SHORT).show(); return true; //這兒無論是true還是false,都會導致OnTouch和OnItemClick不走,因為沒有下面的super來分發事件
}
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);//這兒改為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
DB2+HADR+TSA高可用性測試--DB2HAICU配置TSA實現故障自動切換網絡架構必須先在備節點進行配置 [root@db2-node02 ~]# su - db
深入理解 Android 之 View 的繪制流程,androidview概述 本篇文章會從源碼(基於Android 6.0)角度分析Android中View的繪制流程,
android textview 自動換行 整齊排版,androidtextview一、問題在哪裡? textview顯示長文字時會進行自動折行,如果遇到一些特殊情況,自
利用RecyclerView CardView實現新聞卡片樣式, 引入的包: demo結構: 測試代碼: News.java