今天在做项目过程中遇到在ListView中的Item需要用ListView来展现处理后的内容,然后就遇到了一个很头疼的问题,作为Item的ListView没法进行滑动,而且显示也不正常,只是显示几个子Item。不能将子Item全部显示,但是有些时候我们是想让作为Item的ListView不用全部显示,而是可以进行滑动,要解决这个问题就需要了解一下android对事件的分发机制了,我的解决方案是集成ListView,重写interceptTouchEvent使其返回false来取消父ListView对触摸事件的拦截,将触摸事件分发到子View来处理。然后在使用的时候,将其作为父ListView使用,就可以使子ListView可以滑动了。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
public class ParentListView extends ListView {public ParentListView(Context context) {super(context);// TODO Auto-generated constructor stub}public ParentListView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub}public ParentListView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}//将 onInterceptTouchEvent的返回值设置为false,取消其对触摸事件的处理,将事件分发给子view@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {// TODO Auto-generated method stubreturn false;}} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" ><!-- 这里做demo用,直接使用了android中的ListActivity--> <i.test.ParentListView android:id=" @android :id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:dividerHeight="2dip" android:scrollbars="none" /></LinearLayout> |
原文:http://www.cnblogs.com/small-jian/p/4707015.html