ListView双层嵌套处理

本文介绍了一种解决Android应用中ListView作为Item包含另一个可滑动ListView时出现的滑动和显示问题的方法。通过重写onInterceptTouchEvent方法并返回false,可以取消父ListView对触摸事件的拦截,将事件分发给子ListView处理,从而实现子ListView的滑动功能。

今天在做项目过程中遇到在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
 
@Override
 
public boolean onInterceptTouchEvent(MotionEvent ev) {
 
// TODO Auto-generated method stub
 
return 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>
 

转载于:https://www.cnblogs.com/small-jian/p/4707015.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值