Android 事件分发效果展示及源码

要求:

光标在第一个listview,第一个listview进行滚动;

光标在第二个listview的上半部分,三个listview联动,下半部分,只有第二个listview滚动;

光标在第三个listview,就第三个listview进行滚动.

效果一:

效果二三级联动:

效果二下半部分:

效果三:

做到这个效果我们用到了自定义view:

重写onInterceptTouchEvent拦截方法,返回true

//这里必须返回true,只有返回true 的时候onTouchEvent(MotionEvent event)方法才会被onInterceptTouchEvent调用

重写onTouchEvent消费事件,返回true

    //消费事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        //获取光标所在位置
        int x = (int) event.getX();
        int y = (int) event.getY();

        //获取屏幕宽高
        int width = getWidth();
        int width_x = width / 3;

        int height = getHeight();

        //光标在第一部分
        if (x < width_x) {
            //第一个孩子进行分发
            getChildAt(0).dispatchTouchEvent(event);
            
        //光标在第二部分
        } else if (width_x < x && x < width_x * 2) {

            if (y < height / 2) {
                //三级联动,三个孩子同时进行分发
                getChildAt(0).dispatchTouchEvent(event);
                getChildAt(1).dispatchTouchEvent(event);
                getChildAt(2).dispatchTouchEvent(event);
            } else {
                //第二个孩子进行分发
                getChildAt(1).dispatchTouchEvent(event);
            }
            
        //光标在第三部分
        } else if (x > width_x * 2 && x < width) {
            //第三个孩子进行分发
            getChildAt(2).dispatchTouchEvent(event);
        }
        
        //必须返回true
        return true;
    }

自定义DispatchinnText继承的是LinearLayout作为跟布局:

<?xml version="1.0" encoding="utf-8"?>
<com.example.dispatching_events.DispatchinnText
    android:orientation="horizontal"
    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"
    tools:context=".MainActivity">

    <ListView
        android:background="@android:color/holo_purple"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:entries="@array/arrayData"
        android:layout_height="match_parent">

    </ListView>
    <ListView
        android:background="@android:color/holo_blue_dark"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:entries="@array/arrayData"
        android:layout_height="match_parent">

    </ListView>
    <ListView
        android:background="@android:color/holo_orange_dark"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:entries="@array/arrayData"
        android:layout_height="match_parent">

    </ListView>

</com.example.dispatching_events.DispatchinnText><?xml version="1.0" encoding="utf-8"?>
<com.example.dispatching_events.DispatchinnText
    android:orientation="horizontal"
    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"
    tools:context=".MainActivity">

    <ListView
        android:background="@android:color/holo_purple"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:entries="@array/arrayData"
        android:layout_height="match_parent">

    </ListView>
    <ListView
        android:background="@android:color/holo_blue_dark"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:entries="@array/arrayData"
        android:layout_height="match_parent">

    </ListView>
    <ListView
        android:background="@android:color/holo_orange_dark"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:entries="@array/arrayData"
        android:layout_height="match_parent">

    </ListView>

</com.example.dispatching_events.DispatchinnText>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值