前天在工作中遇到在ListView中的Item需要用ListView来展现处理后的内容,然后就遇到了一个很头疼的问题,作为Item的ListView没法进行滑动,而且显示也不正常,只是显示几个子Item。不能将子Item全部显示,原因是在控件绘制出来之前要对ListView的大小进行计算,要解决将子ListView全部显示出来的问题,就是重新计算一下其大小告知系统即可。后面这个问题比较好解决,网上已经给出解决方案:
前辈们给出了一个方法,重新计算子ListView的大小,然后在设置本ListView的Adapter之后运行这个方法就好了,具体代码如下:
/**
* 设置Listview的高度
*/
public void setListViewHeight(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = ;
for (int i = ; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(, );
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - ));
listView.setLayoutParams(params);
}
但是这个方法设置的item的Layout必须是带有onMeasure()方法的控件,否则在计算的时候会报错,建议使用LinearLayout。
再一个思路相同,但是,不是额外做方法来实现onMeasure()方法的计算LIstView的大小,而是自己继承ListView,重写ListView的onMeasure()方法,来自己计算ListView的高度,然后再xml中直接使用这个自定义的ListView就可以了。
public class MyListView extends ListView {
public MyListView (Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView (Context context) {
super(context);
}
public MyListView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> ,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
这是解决让作为Item的ListView显示全部内容的方案,但是有些时候我们是想让作为Item的ListView不用全部显示,而是可以进行滑动,要解决这个问题就需要了解一下android对事件的分发机制了
我的解决方案是集成ListView,重写interceptTouchEvent使其返回false来取消父ListView对触摸事件的拦截,将触摸事件分发到子View来处理。然后在使用的时候,将其作为父ListView使用,就可以使子ListView可以滑动了。思想来源于下面链接的6楼
http://www.eoeandroid.com/thread-3597-1-1.html具体自定义父ListView代码 :
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;
}
}
xml文件:
<?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>
activity代码如下:
public class ListviewActivity extends ListActivity {
/** Called when the activity is first created. */
private ListView mLv;//这个ListView就是自定义的View
private ParentAdapter adapter;
private final static String[] array = new String[]{"","","","","","","","","","","","","",""};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLv = getListView();
adapter = new ParentAdapter();
mLv.setAdapter(adapter);
}
private class ParentAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return Array.getLength(array);
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return array[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view;
if(position == ){
view = View.inflate(getApplicationContext(), R.layout.item, null);
ListView lv = (ListView) view.findViewById(R.id.lv);
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(ListviewActivity.this, android.R.layout.simple_list_item_1, new String[]{"a","b",
"c","d","e","f","g"});
lv.setAdapter(mAdapter);
}
else{
TextView tv = new TextView(getApplicationContext());
tv.setText(array[position]);
tv.setTextSize();
view = tv;
}
return view;
}
}
}
上面的方法同样适合在ScrollView中嵌套可以滑动View的情况。
后记:2013.04.10
今天登录oschian看到有人提到我,打开消息一看,是对这篇文字的评论,很高兴我写的东西对别人有所帮助。评论人 @jimmy.zhao ,谢谢你让我知道,我帮助了你,这是博客写下去的动力。
这篇文字是在我毕业之后刚入职第二天解决的问题,话说这个问题困扰前面的人有两个月了,我来了之后就把这个坑让我填,前辈说在网上找的所有方案都是已经有牛人给出解决方案。都这么说,但是没有一个人说解决方案是什么,于是让我研究了下。不能说我这篇文字是最早解决这个嵌套滑动问题的,但是如你搜一下解决滑动嵌套问题的帖子基本都在我这篇之后,先自己小骄傲下。
下面说说我在使用这种方法解决了这个问题之后遇到的问题(好像有点绕。。但攻城狮不怕哈),希望能引起后来人的注意:
问题出在一个月之后,根据项目需求,外面的ListView,即父ListView中的条目展示文本数字时需要加入对电话号码和HTTP链接的识别。即:如果是手机号码,点击之后进入拨号盘界面。大家知道,这个很简单,只要在TextView中设置一个简单的属性就好了。然后我的问题就出现,因为父ListView的触摸事件交给了子view,如果子view中的TextView带有这种隐式的点击事件,就会造成父ListView的卡顿现象。而且是相当卡顿。于是在项目中,还是使用了固定子ListView大小,直接使用系统的ListView不再重写父ListView的onInterceptTouchEvent事件。将展示更多,作为加载来处理。还有一个就是在使用TextView的时候,尽量避免使用Html.from()来让TextView支持简单html标签。这个太耗性能。用MAT一看便知,不多说。希望对有时间看本文一眼的人有所帮助。
转自http://my.oschina.net/zhibuji/blog/70892
07 |
public void setListViewHeight(ListView listView) { |
09 |
ListAdapter listAdapter = listView.getAdapter(); |
11 |
if (listAdapter == null ) { |
19 |
for ( int i = 0 ; i < listAdapter.getCount(); i++) { |
21 |
View listItem = listAdapter.getView(i, null , listView); |
23 |
listItem.measure( 0 , 0 ); |
25 |
totalHeight += listItem.getMeasuredHeight(); |
29 |
ViewGroup.LayoutParams params = listView.getLayoutParams(); |
31 |
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1 )); |
33 |
listView.setLayoutParams(params); |
但是这个方法设置的item的Layout必须是带有onMeasure()方法的控件,否则在计算的时候会报错,建议使用LinearLayout。
再一个思路相同,但是,不是额外做方法来实现onMeasure()方法的计算LIstView的大小,而是自己继承ListView,重写ListView的onMeasure()方法,来自己计算ListView的高度,然后再xml中直接使用这个自定义的ListView就可以了。
01 |
public class MyListView extends ListView { |
03 |
public MyListView (Context context, AttributeSet attrs) { |
05 |
super (context, attrs); |
09 |
public MyListView (Context context) { |
15 |
public MyListView (Context context, AttributeSet attrs, int defStyle) { |
17 |
super (context, attrs, defStyle); |
23 |
public void onMeasure( int widthMeasureSpec, int heightMeasureSpec) { |
25 |
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2 , |
29 |
super .onMeasure(widthMeasureSpec, expandSpec); |
这是解决让作为Item的ListView显示全部内容的方案,但是有些时候我们是想让作为Item的ListView不用全部显示,而是可以进行滑动,要解决这个问题就需要了解一下Android对事件的分发机制了
我的解决方案是集成ListView,重写interceptTouchEvent使其返回false来取消父ListView对触摸事件的拦截,将触摸事件分发到子View来处理。然后在使用的时候,将其作为父ListView使用,就可以使子ListView可以滑动了。思想来源于下面链接的6楼
http://www.eoeandroid.com/thread-3597-1-1.html
具体自定义父ListView代码:
01 |
public class ParentListView extends ListView { |
03 |
public ParentListView(Context context) { |
07 |
// TODO Auto-generated constructor stub |
11 |
public ParentListView(Context context, AttributeSet attrs, int defStyle) { |
13 |
super (context, attrs, defStyle); |
15 |
// TODO Auto-generated constructor stub |
19 |
public ParentListView(Context context, AttributeSet attrs) { |
21 |
super (context, attrs); |
23 |
// TODO Auto-generated constructor stub |
26 |
//将 onInterceptTouchEvent的返回值设置为false,取消其对触摸事件的处理,将事件分发给子view |
30 |
public boolean onInterceptTouchEvent(MotionEvent ev) { |
32 |
// TODO Auto-generated method stub |
xml文件:
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" |
activity代码如下:
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" }; |
07 |
public void onCreate(Bundle savedInstanceState) { |
08 |
super .onCreate(savedInstanceState); |
09 |
setContentView(R.layout.main); |
11 |
adapter = new ParentAdapter(); |
12 |
mLv.setAdapter(adapter); |
15 |
private class ParentAdapter extends BaseAdapter{ |
18 |
public int getCount() { |
19 |
// TODO Auto-generated method stub |
20 |
return Array.getLength(array); |
24 |
public Object getItem( int position) { |
25 |
// TODO Auto-generated method stub |
26 |
return array[position]; |
30 |
public long getItemId( int position) { |
31 |
// TODO Auto-generated method stub |
36 |
public View getView( int position, View convertView, ViewGroup parent) { |
37 |
// TODO Auto-generated method stub |
40 |
view = View.inflate(getApplicationContext(), R.layout.item, null ); |
41 |
ListView lv = (ListView) view.findViewById(R.id.lv); |
42 |
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(ListviewActivity. this , android.R.layout.simple_list_item_1, new String[]{ "a" , "b" , |
43 |
"c" , "d" , "e" , "f" , "g" }); |
44 |
lv.setAdapter(mAdapter); |
47 |
TextView tv = new TextView(getApplicationContext()); |
48 |
tv.setText(array[position]); |
上面的方法同样适合在ScrollView中嵌套可以滑动View的情况。
转自http://my.oschina.net/zhibuji/blog/70892
- Android如何在ListView中嵌套ListView
前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个ListItem中放入另外一个ListView.但刚开始的时候,会发现放入的小ListVie ...
- Android -- 在ScrollView中嵌套ListView
在做一个工程,这个工程的布局可以相当的复杂,最外面是ScrollView,在ScrollView里面有两个Listview,这下好了,布局出来了,放在机子上跑,卡得想死有木有,信息乱跑乱出现,表示非常 ...
- 在ListView中嵌套ListView的事件处理
十分感谢此作者,以及作者的作者,让我卡了一星期的问题解决了!!http://blog.csdn.net/hutengfei0701/article/details/8956284谢谢http://my ...
- 项目中那些事|ListView中嵌套ListView问题
要在一个ListView中放入另一个ListView,也即在一个ListView的每个 item 中放入另外一个ListView.但刚开始的时候,会发现放入的子ListView会显示不完全(我这里只显 ...
- 我的Android进阶之旅------>Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法
开发中常常需要自己定义Listview,去继承BaseAdapter,在adapter中按照需求进行编写,问题就出现了,可能会发生点击每一个item的时候没有反应,无法获取的焦点. 如果你的自定义Li ...
- 我的Android进阶之旅------>Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- Android 如何在ScrollView中嵌套ListView
前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个ListItem中放入另外一个ListView.但刚开始的时候,会发现放入的小ListVie ...
- Android实战技巧:如何在ScrollView中嵌套ListView
前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个ListItem中放入另外一个ListView.但刚开始的时候,会发现放入的小ListVie ...
- Android 解决ScrollView下嵌套ListView进页面不在顶部的问题
以下为整理: 方法1 刚开始还可以,后来再调试时就不行了. 为了解决scrollview和listview冲突 设置了listview的高度 结果进页面就不是在顶部了 . 解决方案1:Scrol ...
随机推荐
- actionMode - 在屏幕中的显示位置设置
actionMode 默认的显示位置是在屏幕上方的,如果想要移到下方,可以添加如下属性 在AndroidManifest.xml 的activity中,做如下修改 <activity andro ...
- 1.3 Quick Start中 Step 6: Setting up a multi-broker cluster官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Step 6: Setting up a multi-broker cluster ...
- 1.19 Python基础知识 - 软件目录开发规范及不同模块之间的调用
一个软件项目的开发,除了需要很厉害的开发能力,同时在软件开发项目时,也需要对项目结构有良好的组织能力,将功能进行拆分,不同的功能放在不同的目录或文件中,方便日后的维护,升级等操作.比如核心代码的目录, ...
- Pairs Forming LCM
题目: B - Pairs Forming LCM Time Limit:2000MS Memory Limit:32768KB Description Find the result of ...
- BZOJ2733: [HNOI2012]永无乡(线段树合并)
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
- SpringCloud核心教程 | 第二篇: 使用Intellij中的maven来快速构建Spring Cloud工程
spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环 ...
- android通用JSON解析
ackage cn.com.pcgroup.<a href="http://lib.csdn.net/base/15" class="replace_word&qu ...
- 前端面试题(JavaScript)
(前端面试题大全,持续更新) 箭头函数特点?箭头函数和普通函数的区别 手写懒加载(考虑防抖和重复加载问题) 手写bind(为什么要加预参数,为什么要加new) apply, call, bind ne ...
- xpath使用方法详解id 、starts-with、contains、text()和last() 的用法
1.XPATH使用方法 使用XPATH有如下几种方法定位元素(相比CSS选择器,方法稍微多一点): a.通过绝对路径定位元素(不推荐!) WebElement ele = driver.findEle ...
- Ubuntu14.04中踩过的坑
今天安装Ubuntu 14.0.4,因为需要使用python3,所以就直接配置如下:sudo rm /usr/bin/pythonsudo ln -s /usr/bin/python3.5 /usr ...