ListView和ScrollView都存在滚动的效果,所以一般不建议listView和scrollView进行嵌套使用,但有些需求则需要用到两者嵌套。在android的学习中学了一种事件分发处理机制。
如果listView显示在UI上部,而如imageView等显示在UI下部。
- 首先为listView注册一个滑动监听事件
通过为listView设置固定高度,并注册滑动监听事件。在划动监听事件中,记录用户是否滑动至listView数据底部。
- 通过activity的dispatchTouchEvent()方法进行事件奋发
通过用户记录的是否滑动到listView底部的值在dispatchTouchEvent()方法中进行事件分发处理。
UI :
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <ScrollView 12 android:id="@+id/scrollView1" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:layout_alignParentLeft="true" 16 android:layout_alignParentTop="true" 17 android:layout_alignParentRight="true" 18 android:layout_alignParentBottom="true" 19 > 20 21 <!-- 滚动布局里只能包含一个线性布局 --> 22 <LinearLayout 23 android:layout_width="match_parent" 24 android:layout_height="match_parent" 25 android:orientation="vertical" > 26 27 <ListView 28 android:id="@+id/listView1" 29 android:layout_width="match_parent" 30 android:layout_height="430dp" > 31 </ListView> 32 <ImageView 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:src="@drawable/a"/> 36 <ImageView 37 android:src="@drawable/a" 38 android:layout_width="wrap_content" 39 android:layout_height="wrap_content"/> 40 <ImageView 41 android:src="@drawable/a" 42 android:layout_width="wrap_content" 43 android:layout_height="wrap_content"/> 44 <ImageView 45 android:src="@drawable/a" 46 android:layout_width="wrap_content" 47 android:layout_height="wrap_content"/> 48 49 </LinearLayout> 50 </ScrollView> 51 52 </RelativeLayout>