多个Listview瀑布流效果
效果展示

原理解释
自定义MyLinearLayout,继承至LinearLayout,在布局文件中,将3个listview放置在MyLinearLayout中。
重写MyLinearLayout中的onInterceptTouchEvent方法,返回true,打断向listview传递的触摸事件。
重写onTouchEvent方法,根据触摸位置,将触摸事件通过调用子view的dispatchTouchEvent方法,传递给相应位置的listview。
listview接受到触摸事件后就可以自行处理相关的滑动逻辑。
代码实现
界面布局
<jamffy.example.waterfalllistview.MyLinearLayout xmlns:android="http:// schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="jamffy.example.waterfalllistview.MainActivity" >
<ListView
android:id="@+id/lv1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:divider="@null"
android:dividerHeight="5dp"
android:scrollbars="none" >
</ListView>
<ListView
android:id="@+id/lv2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:divider="@null"
android:dividerHeight="5dp"
android:scrollbars="none" >
</ListView>
<ListView
android:id="@+id/lv3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:divider="@null"
android:dividerHeight="5dp"
android:scrollbars="none" >
</ListView>
</jamffy.example.waterfalllistview.MyLinearLayout>
自定义控件
/**
* @author tmac 如果不做处理MyLinearLayout中的子view能自行处理touch事件。
* 现在我希望当我在屏幕中间上方拖动时,整个屏幕的子view一起向上拖动,
* 这是就需要在满足条件时,中断该touch事件,交给MyLinearLayout这个父view来处理。
* 先中断所有子view的touch事件,然后根据触摸的位置,有父view把点击事件分发给相应的子view。
* 在分发之前需要给touch事件的对象event重新设置位置,因为子view的坐标系与父view是不同的。
*/
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context) {
super(context);
}
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int count = getChildCount();
int width = getWidth() / count;
int height = getHeight();
int currX = (int) event.getX();
int currY = (int) event.getY();
if (currX < width) {
event.setLocation(width / 2, currY);
getChildAt(0).dispatchTouchEvent(event);
return true;
} else if (currX < 2 * width) {
if (currY > height / 2) {
event.setLocation(width / 2, currY);
getChildAt(1).dispatchTouchEvent(event);
return true;
} else {
event.setLocation(width, currY);
for(int i=0;i<count;i++){
getChildAt(i).dispatchTouchEvent(event);
}
return true;
}
} else if (currX < 3 * width) {
event.setLocation(width / 2, currY);
getChildAt(2).dispatchTouchEvent(event);
return true;
}
return true;
}
}
完整代码
github