记得以前面试的时候,面试官问了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;
}
});
}
运行效果如下:
##点击源码下载