事件分发
dispatchTouchEvent-向下分发操作
onInterceptTouchEvent-拦截操作(1.Activity和TextView没有,Layout有;2.true-拦截、false-不拦截)
onTouchEvent-处理操作(true-处理、false-不处理)
1.textview点击事件
<org.mobiletrain.touchevent.MyLinearLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:background="#ff0303"
android:gravity="center">
<org.mobiletrain.touchevent.MyTextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#18ff03"/>
</org.mobiletrain.touchevent.MyLinearLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.d("lenve", "MainActivity - dispatchTouchEvent: ");
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("lenve", "MainActivity - onTouchEvent: ");
return super.onTouchEvent(event);
}
}
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context) {
super(context);
}
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//事件分发
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.d("lenve", "MyLinearLayout - dispatchTouchEvent: ");
return super.dispatchTouchEvent(ev);
}
//事件拦截
//1.返回值为false表示不拦截事件,事件继续向下分发给它的子控件
//2.返回值为true表示拦截事件,拦截之后自己处理
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean b = super.onInterceptTouchEvent(ev);
Log.d("lenve", "MyLinearLayout - onInterceptTouchEvent: " + b);
return b;
}
//事件处理
//1.false表示该事件未处理,向上分发给它的父容器或者Activity
//2.true表示该事件已处理,事件到此结束
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean b = super.onTouchEvent(event);
Log.d("lenve", "MyLinearLayout - onTouchEvent: " + b);
return b;
}
}
public class MyTextView extends TextView {
public MyTextView(Context context) {
super(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
Log.d("lenve", "MyTextView - dispatchTouchEvent: ");
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("lenve", "MyTextView - onTouchEvent: ");
return super.onTouchEvent(event);
}
}
结果:
//按下
04-25 12:44:10.339 27977-27977/com.example.administrator.touchevent D/lenve: MainActivity - dispatchTouchEvent:
04-25 12:44:10.339 27977-27977/com.example.administrator.touchevent D/lenve: MyLinearLayout - dispatchTouchEvent:
04-25 12:44:10.339 27977-27977/com.example.administrator.touchevent D/lenve: MyLinearLayout - onInterceptTouchEvent:
04-25 12:44:10.339 27977-27977/com.example.administrator.touchevent D/levne: MyTestView - dispatchTouchEvent:
04-25 12:44:10.339 27977-27977/com.example.administrator.touchevent D/lenve: MyTestView - onTouchEvent:
04-25 12:44:10.339 27977-27977/com.example.administrator.touchevent D/lenve: MyLinearLayout - onTouchEvent:
04-25 12:44:10.339 27977-27977/com.example.administrator.touchevent D/lenve: MainActivity - onTouchEvent:
//松开
04-25 12:44:11.103 27977-27977/com.example.administrator.touchevent D/lenve: MainActivity - dispatchTouchEvent:
04-25 12:44:11.103 27977-27977/com.example.administrator.touchevent D/lenve: MainActivity - onTouchEvent:
2.listview点击事件
public class MainActivity extends AppCompatActivity {
private List<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.lv);
initData();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, list.get(position), Toast.LENGTH_SHORT).show();
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
list.remove(position);
adapter.notifyDataSetChanged();
return true;
}
});
}
private void initData() {
list = new ArrayList<>();
for (int i = 0; i < 30; i++) {
list.add("李四:" + i);
}
}
}
注:按下后,setOnItemLongClickListener先执行,松开后,执行onItemClick。设置setOnItemLongClickListener返回true,松开后就不会执行onItemClick
3.viewpager滑动
viewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//让ViewPager的父容器不要拦截滑动事件
viewPager.getParent().requestDisallowInterceptTouchEvent(true);
//这里如果返回true则ViewPager的滑动事件会被屏蔽
return false;
}
});
结果:
第一页的广告滑到最右边再滑不会切换到第二页