Android ScrollView中的ListView不能滑动

首先来看一个实例:
Activity中的代码:

private void initView() {
        ListView listView = (ListView)findViewById(R.id.lv);
        ArrayList list  = new ArrayList();
        for(int i=0;i<30;i++){
            list.add("item"+i);
        }
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
        listView.setAdapter(adapter);
    }

xml中的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <ListView
                android:id="@+id/lv"
                android:layout_width="wrap_content"
                android:layout_height="200dp">

            </ListView>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="1000dp"
                android:text="看我干啥,找挨揍啊!"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="没脸啊 还看啊"/>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

运行效果:
这里写图片描述

可以发现,listView中的条目有很多,没能显示全,想滚动ListView查看更多条目,却发现事件被ScrollView拦截。解决的办法有两种,都是通过自定义父布局ScrollView。

1 在dispatchTouchEvent()方法中调用 requestDisallowInterceptTouchEvent(true)方法。

public class MyScrollView extends ScrollView {

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

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        requestDisallowInterceptTouchEvent(true);
        return super.dispatchTouchEvent(ev);
    }
}

2 或者时采用重写onInterceptTouchEvent()方法并返回false。

public class MyScrollView extends ScrollView {

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

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return false;
    }
  }  

上述两种方式都可以,然后在布局中引入自定义的ScrollView。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.example.practice_click.MyScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <ListView
                android:id="@+id/lv"
                android:layout_width="wrap_content"
                android:layout_height="200dp">

            </ListView>
            <TextView
                android:layout_marginTop="1000dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="看我干啥,找挨揍啊!"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="没脸啊 还看啊"/>
        </LinearLayout>
    </com.example.practice_click.MyScrollView>
</LinearLayout>

效果图
这里写图片描述
可以看到滑动ListView时候ScrollView不在滚动。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值