在ListView中嵌套ListView的事件处理
2016-04-07 10:32
295 查看
转载地址:/article/3488608.html
前天在工作中遇到在ListView中的Item需要用ListView来展现处理后的内容,然后就遇到了一个很头疼的问题,作为Item的ListView没法进行滑动,而且显示也不正常,只是显示几个子Item。不能将子Item全部显示,原因是在控件绘制出来之前要对ListView的大小进行计算,要解决将子ListView全部显示出来的问题,就是重新计算一下其大小告知系统即可。后面这个问题比较好解决,网上已经给出解决方案:
前辈们给出了一个方法,重新计算子ListView的大小,然后在设置本ListView的Adapter之后运行这个方法就好了,具体代码如下:
但是这个方法设置的item的Layout必须是带有onMeasure()方法的控件,否则在计算的时候会报错,建议使用LinearLayout。
再一个思路相同,但是,不是额外做方法来实现onMeasure()方法的计算LIstView的大小,而是自己继承ListView,重写ListView的onMeasure()方法,来自己计算ListView的高度,然后再xml中直接使用这个自定义的ListView就可以了。
这是解决让作为Item的ListView显示全部内容的方案,但是有些时候我们是想让作为Item的ListView不用全部显示,而是可以进行滑动,要解决这个问题就需要了解一下android对事件的分发机制了
我的解决方案是集成ListView,重写interceptTouchEvent使其返回false来取消父ListView对触摸事件的拦截,将触摸事件分发到子View来处理。然后在使用的时候,将其作为父ListView使用,就可以使子ListView可以滑动了。思想来源于下面链接的6楼
http://www.eoeandroid.com/thread-3597-1-1.html
具体自定义父ListView代码:
xml文件:
activity代码如下:
上面的方法同样适合在ScrollView中嵌套可以滑动View的情况。
前天在工作中遇到在ListView中的Item需要用ListView来展现处理后的内容,然后就遇到了一个很头疼的问题,作为Item的ListView没法进行滑动,而且显示也不正常,只是显示几个子Item。不能将子Item全部显示,原因是在控件绘制出来之前要对ListView的大小进行计算,要解决将子ListView全部显示出来的问题,就是重新计算一下其大小告知系统即可。后面这个问题比较好解决,网上已经给出解决方案:
前辈们给出了一个方法,重新计算子ListView的大小,然后在设置本ListView的Adapter之后运行这个方法就好了,具体代码如下:
01 | /** |
02 |
03 | * 设置Listview的高度 |
04 |
05 | */ |
06 |
07 | public void setListViewHeight(ListView listView) { |
08 |
09 | ListAdapter listAdapter = listView.getAdapter(); |
10 |
11 | if (listAdapter == null ) { |
12 |
13 | return ; |
14 |
15 | } |
16 |
17 | int totalHeight = 0 ; |
18 |
19 | for ( int i = 0 ; i < listAdapter.getCount(); i++) { |
20 |
21 | View listItem = listAdapter.getView(i, null , |
22 |
23 | listItem.measure( 0 , 0 ); |
24 |
25 | totalHeight |
26 |
27 | } |
28 |
29 | ViewGroup.LayoutParams |
30 |
31 | params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1 )); |
32 |
33 | listView.setLayoutParams(params); |
34 |
35 | } |
再一个思路相同,但是,不是额外做方法来实现onMeasure()方法的计算LIstView的大小,而是自己继承ListView,重写ListView的onMeasure()方法,来自己计算ListView的高度,然后再xml中直接使用这个自定义的ListView就可以了。
01 | public class MyListView extends ListView { |
02 |
03 | public MyListView |
04 |
05 | super (context, attrs); |
06 |
07 | } |
08 |
09 | public MyListView (Context context) { |
10 |
11 | super (context); |
12 |
13 | } |
14 |
15 | public MyListView int defStyle) { |
16 |
17 | super (context, |
18 |
19 | } |
20 |
21 | @Override |
22 |
23 | public void onMeasure( int widthMeasureSpec, int heightMeasureSpec) { |
24 |
25 | int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2 , |
26 |
27 | MeasureSpec.AT_MOST); |
28 |
29 | super .onMeasure(widthMeasureSpec, expandSpec); |
30 |
31 | } |
32 |
33 | } |
我的解决方案是集成ListView,重写interceptTouchEvent使其返回false来取消父ListView对触摸事件的拦截,将触摸事件分发到子View来处理。然后在使用的时候,将其作为父ListView使用,就可以使子ListView可以滑动了。思想来源于下面链接的6楼
http://www.eoeandroid.com/thread-3597-1-1.html
具体自定义父ListView代码:
01 | public class ParentListView extends ListView { |
02 |
03 | public ParentListView(Context context) { |
04 |
05 | super (context); |
06 |
07 | // TODO Auto-generated constructor stub |
08 |
09 | } |
10 |
11 | public ParentListView(Context int defStyle) { |
12 |
13 | super (context, |
14 |
15 | // TODO Auto-generated constructor stub |
16 |
17 | } |
18 |
19 | public ParentListView(Context |
20 |
21 | super (context, attrs); |
22 |
23 | // TODO Auto-generated constructor stub |
24 |
25 | } |
26 | //将 onInterceptTouchEvent的返回值设置为false,取消其对触摸事件的处理,将事件分发给子view |
27 |
28 | @Override |
29 |
30 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
31 |
32 | // TODO Auto-generated method stub |
33 |
34 | return false ; |
35 |
36 | } |
37 |
38 | } |
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:layout_width = "fill_parent" |
04 | android:layout_height = "fill_parent" |
05 | android:orientation = "vertical" > |
06 | <!-- 这里做demo用,直接使用了android中的ListActivity--> |
07 | < i.test.ParentListView android:id = " @android :id/list" |
08 | android:layout_width = "fill_parent" |
09 | android:layout_height = "fill_parent" |
10 | android:dividerHeight = "2dip" |
11 | android:scrollbars = "none" |
12 | /> |
13 |
14 | </ LinearLayout > |
01 | public class ListviewActivity extends ListActivity { |
02 | /** Called when the activity is first created. */ |
03 | private ListView mLv; //这个ListView就是自定义的View |
04 | private ParentAdapter adapter; |
05 | private final static String[] array = new String[]{ "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "11" , "12" , "13" , "14" }; |
06 | @Override |
07 | public void onCreate(Bundle savedInstanceState) { |
08 | super .onCreate(savedInstanceState); |
09 | setContentView(R.layout.main); |
10 | mLv = getListView(); |
11 | adapter = new ParentAdapter(); |
12 | mLv.setAdapter(adapter); |
13 |
14 | } |
15 | private class ParentAdapter extends BaseAdapter{ |
16 |
17 | @Override |
18 | public int getCount() { |
19 | // TODO Auto-generated method stub |
20 | return Array.getLength(array); |
21 | } |
22 |
23 | @Override |
24 | public Object getItem( int position) { |
25 | // TODO Auto-generated method stub |
26 | return array[position]; |
27 | } |
28 |
29 | @Override |
30 | public long getItemId( int position) { |
31 | // TODO Auto-generated method stub |
32 | return position; |
33 | } |
34 |
35 | @Override |
36 | public View getView( int position, |
37 | // TODO Auto-generated method stub |
38 | View view; |
39 | if (position == 5 ){ |
40 | view null ); |
41 | ListView lv = (ListView) view.findViewById(R.id.lv); |
42 | ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(ListviewActivity. this , new String[]{ "a" , "b" , |
43 | "c" , "d" , "e" , "f" , "g" }); |
44 | lv.setAdapter(mAdapter); |
45 | } |
46 | else { |
47 | TextView tv = new TextView(getApplicationContext()); |
48 | tv.setText(array[position]); |
49 | tv.setTextSize( 30 ); |
50 | view = tv; |
51 | } |
52 | return view; |
53 | } |
54 |
55 | } |
56 | } |
相关文章推荐
- C++ map嵌套使用 链接
- PLSQL中文乱码问题
- hdu 1160 FatMouse's Speed
- iOS - 策略模式
- 书单(二)Head First HTML与CSS、XHTML(中文版).pdf
- view动画浅析
- 硬件免费,乐视正在试图变革消费模式
- 硬件免费,乐视正在试图变革消费模式
- java 上的各种路径获取
- WEB开发者必备的7个JavaScript函数
- Main函数中的argc和argv应用举例
- 设置Tomcat的UTF-8编码
- BZOJ 3262 陌上花开(CDQ分治)
- 彻底明白如何设置minSdkVersion和targetSdkVersion
- 42、Oracle 11g服务器安装详细步骤——图文教程
- VS应用程序无法启动0xc000007b
- Csdn 转载方法
- AD PCB制图问题解答
- android studio自定义属性使用
- 4.1.3.2 master_sigsetup函数:信号处理程序初始化