博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
事件分发
阅读量:5159 次
发布时间:2019-06-13

本文共 5404 字,大约阅读时间需要 18 分钟。

dispatchTouchEvent-向下分发操作

onInterceptTouchEvent-拦截操作(1.Activity和TextView没有,Layout有;2.true-拦截、false-不拦截)

onTouchEvent-处理操作(true-处理、false-不处理)

1.textview点击事件

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
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
adapter = new ArrayAdapter
(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;    }});

结果:

第一页的广告滑到最右边再滑不会切换到第二页

转载于:https://www.cnblogs.com/anni-qianqian/p/5430300.html

你可能感兴趣的文章
基于wxPython的python代码统计工具
查看>>
淘宝JAVA中间件Diamond详解(一)---简介&快速使用
查看>>
Hadoop HBase概念学习系列之HBase里的宽表设计概念(表设计)(二十七)
查看>>
Kettle学习系列之Kettle能做什么?(三)
查看>>
Day03:Selenium,BeautifulSoup4
查看>>
awk变量
查看>>
mysql_对于DQL 的简单举例
查看>>
35. Search Insert Position(C++)
查看>>
[毕业生的商业软件开发之路]C#异常处理
查看>>
一些php文件函数
查看>>
有关快速幂取模
查看>>
Linux运维必备工具
查看>>
字符串的查找删除
查看>>
NOI2018垫底记
查看>>
快速切题 poj 1002 487-3279 按规则处理 模拟 难度:0
查看>>
Codeforces Round #277 (Div. 2)
查看>>
【更新】智能手机批量添加联系人
查看>>
NYOJ-128前缀式计算
查看>>
淡定,啊。数据唯一性
查看>>
深入理解 JavaScript 事件循环(一)— event loop
查看>>