Android ScrollView中的ListView不能滑动

来源:互联网 发布:叶罗丽娃娃淘宝 编辑:程序博客网 时间:2024/05/21 20:27

首先来看一个实例:
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不在滚动。

0 0
原创粉丝点击