android viewGroup事件分发一

来源:互联网 发布:mac合上盖子播放音乐 编辑:程序博客网 时间:2024/06/01 20:03

android viewGroup事件分发

MainActivity

import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.MotionEvent;import android.view.View;public class MainActivity extends AppCompatActivity {    private MyButton button;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (MyButton) findViewById(R.id.my_btn);        button.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View view, MotionEvent motionEvent) {                int action = motionEvent.getAction();                switch (action){                    case MotionEvent.ACTION_DOWN:                        Log.i("event", "MainActivity-------onTouch MotionEvent.ACTION_DOWN");                        break;                    case MotionEvent.ACTION_MOVE:                        Log.i("event", "MainActivity-------onTouch MotionEvent.ACTION_MOVE");                        break;                    case MotionEvent.ACTION_UP:                        Log.i("event", "MainActivity-------onTouch MotionEvent.ACTION_UP");                        break;                }                return false;            }        });    }}

MyButton

import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.widget.Button;/** * Created by Administrator on 2016/3/25. */public class MyButton extends Button{    public MyButton(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        int action = event.getAction();        switch (action){            case MotionEvent.ACTION_DOWN:                Log.i("event", "MyButton---------------onTouchEvent ACTION_DOWN");                break;            case MotionEvent.ACTION_MOVE:                Log.i("event", "MyButton---------------onTouchEvent ACTION_MOVE");                break;            case MotionEvent.ACTION_UP:                Log.i("event", "MyButton---------------onTouchEvent ACTION_UP");                break;        }        return super.onTouchEvent(event);    }    @Override    public boolean dispatchTouchEvent(MotionEvent event) {        int action = event.getAction();        switch (action){            case MotionEvent.ACTION_DOWN:                Log.i("event", "MyButton---------------dispatchTouchEvent ACTION_DOWN");                break;            case MotionEvent.ACTION_MOVE:                Log.i("event", "MyButton---------------dispatchTouchEvent ACTION_MOVE");                break;            case MotionEvent.ACTION_UP:                Log.i("event", "MyButton---------------dispatchTouchEvent ACTION_UP");                break;        }        return super.dispatchTouchEvent(event);    }}

MyLinearLayout

import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.widget.LinearLayout;/** * Created by Administrator on 2016/4/18. */public class MyLinearLayout extends LinearLayout{    public MyLinearLayout(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    public boolean dispatchTouchEvent(MotionEvent ev) {        int action = ev.getAction();        switch (action){            case MotionEvent.ACTION_DOWN:                Log.e("event", "MyLinearLayout--------->dispatchTouchEvent ACTION_DOWN");                break;            case MotionEvent.ACTION_MOVE:                Log.e("event", "MyLinearLayout--------->dispatchTouchEvent ACTION_MOVE");                break;            case MotionEvent.ACTION_UP:                Log.e("event", "MyLinearLayout--------->dispatchTouchEvent ACTION_UP");                break;        }        return super.dispatchTouchEvent(ev);    }    @Override    public boolean onTouchEvent(MotionEvent event)    {        int action = event.getAction();        switch (action)        {            case MotionEvent.ACTION_DOWN:                Log.e("event", "MyLinearLayout--------->onTouchEvent ACTION_DOWN");                break;            case MotionEvent.ACTION_MOVE:                Log.e("event", "MyLinearLayout--------->onTouchEvent ACTION_MOVE");                break;            case MotionEvent.ACTION_UP:                Log.e("event", "MyLinearLayout--------->onTouchEvent ACTION_UP");                break;            default:                break;        }        return super.onTouchEvent(event);    }    @Override    public boolean onInterceptTouchEvent(MotionEvent ev)    {        int action = ev.getAction();        switch (action)        {            case MotionEvent.ACTION_DOWN:                Log.e("event", "MyLinearLayout--------->onInterceptTouchEvent ACTION_DOWN");                break;            case MotionEvent.ACTION_MOVE:                Log.e("event", "MyLinearLayout--------->onInterceptTouchEvent ACTION_MOVE");                break;            case MotionEvent.ACTION_UP:                Log.e("event", "MyLinearLayout--------->onInterceptTouchEvent ACTION_UP");                break;            default:                break;        }        return super.onInterceptTouchEvent(ev);    }    @Override    public void requestDisallowInterceptTouchEvent(boolean disallowIntercept)    {        Log.e("event", "MyLinearLayout--------->requestDisallowInterceptTouchEvent ");        super.requestDisallowInterceptTouchEvent(disallowIntercept);    }}

activity_main

<?xml version="1.0" encoding="utf-8"?><himici.androidevent.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:fitsSystemWindows="true"    tools:context="himici.androidevent.MainActivity">   <himici.androidevent.MyButton       android:id="@+id/my_btn"       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:layout_gravity="center"       android:text="test click"/></himici.androidevent.MyLinearLayout>

当点击自定义按钮

//点击按钮04-18 17:38:31.018 32320-32320/? E/event: MyLinearLayout--------->dispatchTouchEvent ACTION_DOWN04-18 17:38:31.018 32320-32320/? E/event: MyLinearLayout--------->onInterceptTouchEvent ACTION_DOWN04-18 17:38:31.019 32320-32320/? I/event: MyButton---------------dispatchTouchEvent ACTION_DOWN04-18 17:38:31.019 32320-32320/? I/event: MainActivity-------onTouch MotionEvent.ACTION_DOWN04-18 17:38:31.019 32320-32320/? I/event: MyButton---------------onTouchEvent ACTION_DOWN04-18 17:38:31.033 32320-32320/? E/event: MyLinearLayout--------->dispatchTouchEvent ACTION_MOVE04-18 17:38:31.033 32320-32320/? E/event: MyLinearLayout--------->onInterceptTouchEvent ACTION_MOVE04-18 17:38:31.033 32320-32320/? I/event: MyButton---------------dispatchTouchEvent ACTION_MOVE04-18 17:38:31.034 32320-32320/? I/event: MainActivity-------onTouch MotionEvent.ACTION_MOVE04-18 17:38:31.034 32320-32320/? I/event: MyButton---------------onTouchEvent ACTION_MOVE04-18 17:38:31.206 32320-32320/? E/event: MyLinearLayout--------->dispatchTouchEvent ACTION_UP04-18 17:38:31.207 32320-32320/? E/event: MyLinearLayout--------->onInterceptTouchEvent ACTION_UP04-18 17:38:31.207 32320-32320/? I/event: MyButton---------------dispatchTouchEvent ACTION_UP04-18 17:38:31.207 32320-32320/? I/event: MainActivity-------onTouch MotionEvent.ACTION_UP04-18 17:38:31.207 32320-32320/? I/event: MyButton---------------onTouchEvent ACTION_UP

点击空白处

// 点击空白处04-18 17:38:49.437 32320-32320/? E/event: MyLinearLayout--------->dispatchTouchEvent ACTION_DOWN04-18 17:38:49.437 32320-32320/? E/event: MyLinearLayout--------->onInterceptTouchEvent ACTION_DOWN04-18 17:38:49.437 32320-32320/? E/event: MyLinearLayout--------->onTouchEvent ACTION_DOWN
1 0
原创粉丝点击