利用事件分发机制解决ScrollView嵌套ListView滑动冲突

记得以前面试的时候,面试官问了ScrollView嵌套ListView使用的问题。那么ScrollView嵌套ListView使用会出现什么效果呢?
如布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:fitsSystemWindows="true"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"   tools:context="com.wzc.example.administrator.demo.ThirdActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="false">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher" />
            

            <ListView
                android:id="@+id/list_view2"
                android:background="#00ffff"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher" />

        </LinearLayout>
    </ScrollView>
</RelativeLayout>

效果如下:

ListView只会显示一行,且ListView不能滑动。

那么怎么样才能实现ListView和ScrollView滑动互不影响呢?
其实方法有很多种,我这里讲的是利用事件分发机制实现。
####**第一种方法:**自定义父层View(这里是ScrollView)并重写onInterceptTouchEvent()方法

public class MyScrollView extends ScrollView {
    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    //不拦截,继续分发下去
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return false;
    }
}

将MyScrollView替代布局中的ScrollView,并设置ListView的宽高。

<com.wzc.example.administrator.demo.view.MyScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="false">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher" />


            <ListView
                android:id="@+id/list_view2"
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:background="#00ffff" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher" />

        </LinearLayout>
    </com.wzc.example.administrator.demo.view.MyScrollView>

运行结果如下:

####第二种方法: 自定义子View(这里是ListView)并重写dispatchTouchEvent()方法,通知通知父层ViewGroup不截获 (要在布局中设置lisetview高度,不然任然是显示一条item)。
代码如下:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) { 
    //通知父层ViewGroup不截获  
    getParent().requestDisallowInterceptTouchEvent(true);  
    return super.dispatchTouchEvent(ev);    
}  

运行效果如下:

####第三种方法: 设置setOnTouchListener监听
代码如下:

//当用户按下的时候,我们告诉父组件,不要拦截我的事件(这个时候子组件是可以正常响应事件的),拿起之后就会告诉父组件可以阻止。
 mListView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_MOVE:                     v.getParent().requestDisallowInterceptTouchEvent(true);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:                        v.getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                }
                return false;
            }
        }); 
 }

运行效果如下:

##点击源码下载

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值