事件拦截和分发demo


实现效果:页面上竖直摆放3个listview,滑动左边和右边的listview正常滑动,滑动 中间listview 时 滑动整个屏幕。

这个效果实现 需要自定义一个layout,然后对事件做处理即可。

布局引用:

<com.example.pinterestlistview.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mll"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/lv2"
        android:scrollbars="none"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ListView
        android:id="@+id/lv1"
        android:scrollbars="none"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ListView
        android:id="@+id/lv3"
        android:scrollbars="none"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</com.example.pinterestlistview.MyLinearLayout>

代码注释:

package com.example.pinterestlistview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

public class MyLinearLayout extends LinearLayout {


	public MyLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		//返回true 表示Intercept (中断)此事件,这里可以自定义我的事件。
		//返回false 表示不打断。事件向子view传递。
		return true;
	}

	//将屏幕竖直平分为三个区域。第一和第三个区域可以自由滑动,第二个区域滑动上半部分 可以拖动整个屏幕,下半部分 滑动第二个区域。
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		//计算child的宽度,由于三个listview 是平分屏幕宽度的,因此可以根据具体布局情况来计算。
		int width=getWidth()/getChildCount();
		
		// 计算child的宽度(根据具体布局情况来计算)
		int height = getHeight(); 
		
		//以上:计算子view的宽高。
		
		int count=getChildCount(); 
		
		float eventX = event.getX();
		
		if (eventX<width){	// 滑动左边的 listView
			// 将事件定位于view控件上,这个位置(width/2, event.getY())就是getChildAt(0) 的区域。
			event.setLocation(width/2, event.getY()); 
			getChildAt(0).dispatchTouchEvent(event); // 分发 事件给子view
			return true; // 消耗掉当前事件
			
		} else if (eventX > width && eventX < 2 * width) { //滑动中间的 listView  ,事件的位置在中间listview位置。
			float eventY = event.getY();
			if (eventY < height / 2) {
				event.setLocation(width / 2, event.getY());
//				getChildAt(1).dispatchTouchEvent(event);  // 阻止掉这块区域滑动
				for (int i = 0; i < count; i++) {
					View child = getChildAt(i);
					try {
						child.dispatchTouchEvent(event);  //把事件 分发给所有的listview
					} catch (Exception e) {
						e.printStackTrace();
					}
					
				}
				return true;
			} else if (eventY > height / 2) {  //滑动右边的区域
				event.setLocation(width / 2, event.getY());
				try {
					getChildAt(1).dispatchTouchEvent(event); // 把事件只传递给中间的listview
				} catch (Exception e) {
					e.printStackTrace();
				}
				return true;
			}
		}else if (eventX>2*width){
			event.setLocation(width/2, event.getY());   
			getChildAt(2).dispatchTouchEvent(event);     把事件只传递给右边的listview
			return true;
		}
		
		return true;
	}
	
}







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值