要求:
光标在第一个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>