前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >多个Listview瀑布流效果

多个Listview瀑布流效果

作者头像
felix
发布2018-06-08 11:32:53
1.2K0
发布2018-06-08 11:32:53
举报
文章被收录于专栏:Felix的技术分享

多个Listview瀑布流效果

效果展示

原理解释

代码语言:javascript
复制
自定义MyLinearLayout,继承至LinearLayout,在布局文件中,将3个listview放置在MyLinearLayout中。
重写MyLinearLayout中的onInterceptTouchEvent方法,返回true,打断向listview传递的触摸事件。
重写onTouchEvent方法,根据触摸位置,将触摸事件通过调用子view的dispatchTouchEvent方法,传递给相应位置的listview。
listview接受到触摸事件后就可以自行处理相关的滑动逻辑。

代码实现

界面布局

代码语言:javascript
复制
    <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>

自定义控件

代码语言:javascript
复制
    /**
     * @author tmac 如果不做处理MyLinearLayout中的子view能自行处理touch事件。
     *         现在我希望当我在屏幕中间上方拖动时,整个屏幕的子view一起向上拖动,
     *         这是就需要在满足条件时,中断该touch事件,交给MyLinearLayout这个父view来处理。
     *         先中断所有子view的touch事件,然后根据触摸的位置,有父view把点击事件分发给相应的子view。
     *         在分发之前需要给touch事件的对象event重新设置位置,因为子view的坐标系与父view是不同的。
     */

    // 这个类专门为三个子listview服务。
    public class MyLinearLayout extends LinearLayout {

    public MyLinearLayout(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public MyLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    @Override
    // 返回true中断点击事件
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    // 用分发的方式决定哪个子view可以收到点击事件
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int count = getChildCount();
        // 每个子view的宽度
        int width = getWidth() / count;
        int height = getHeight();

        // 当前触摸的位置
        int currX = (int) event.getX();
        int currY = (int) event.getY();
        // 判断位置
        if (currX < width) { // 处理最左边的逻辑
            // 在分发之前需要给touch事件的对象event重新设置位置,因为子view的坐标系与父view是不同的。
            event.setLocation(width / 2, currY);
            getChildAt(0).dispatchTouchEvent(event);
            return true;
        } else if (currX < 2 * width) { // 处理中间的逻辑
            if (currY > height / 2) { // 如果在下方,只移动中间的子view
                event.setLocation(width / 2, currY);
                getChildAt(1).dispatchTouchEvent(event);
                return true;
            } else {// 如果在上方,拖动三个view
                event.setLocation(width, currY);
            // 同时把touch事件分发给三个子view
                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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015年04月19日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 多个Listview瀑布流效果
    • 效果展示
      • 原理解释
        • 代码实现
          • 界面布局
          • 自定义控件
          • 完整代码
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档