android viewGroup事件分发一

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_DOWN
04-18 17:38:31.018 32320-32320/? E/event: MyLinearLayout--------->onInterceptTouchEvent ACTION_DOWN
04-18 17:38:31.019 32320-32320/? I/event: MyButton---------------dispatchTouchEvent ACTION_DOWN
04-18 17:38:31.019 32320-32320/? I/event: MainActivity-------onTouch MotionEvent.ACTION_DOWN
04-18 17:38:31.019 32320-32320/? I/event: MyButton---------------onTouchEvent ACTION_DOWN

04-18 17:38:31.033 32320-32320/? E/event: MyLinearLayout--------->dispatchTouchEvent ACTION_MOVE
04-18 17:38:31.033 32320-32320/? E/event: MyLinearLayout--------->onInterceptTouchEvent ACTION_MOVE
04-18 17:38:31.033 32320-32320/? I/event: MyButton---------------dispatchTouchEvent ACTION_MOVE
04-18 17:38:31.034 32320-32320/? I/event: MainActivity-------onTouch MotionEvent.ACTION_MOVE
04-18 17:38:31.034 32320-32320/? I/event: MyButton---------------onTouchEvent ACTION_MOVE

04-18 17:38:31.206 32320-32320/? E/event: MyLinearLayout--------->dispatchTouchEvent ACTION_UP
04-18 17:38:31.207 32320-32320/? E/event: MyLinearLayout--------->onInterceptTouchEvent ACTION_UP
04-18 17:38:31.207 32320-32320/? I/event: MyButton---------------dispatchTouchEvent ACTION_UP
04-18 17:38:31.207 32320-32320/? I/event: MainActivity-------onTouch MotionEvent.ACTION_UP
04-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_DOWN
04-18 17:38:49.437 32320-32320/? E/event: MyLinearLayout--------->onInterceptTouchEvent ACTION_DOWN
04-18 17:38:49.437 32320-32320/? E/event: MyLinearLayout--------->onTouchEvent ACTION_DOWN
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值