前天在工作中遇到在ListView中的Item需要用ListView来展现处理后的内容,然后就遇到了一个很头疼的问题,作为Item的ListView没法进行滑动,而且显示也不正常,只是显示几个子Item。不能将子Item全部显示,原因是在控件绘制出来之前要对ListView的大小进行计算,要解决将子ListView全部显示出来的问题,就是重新计算一下其大小告知系统即可。后面这个问题比较好解决,网上已经给出解决方案:
前辈们给出了一个方法,重新计算子ListView的大小,然后在设置本ListView的Adapter之后运行这个方法就好了,具体代码如下:
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) {
|
11 |
public ParentListView(Context context, AttributeSet attrs, int defStyle) {
|
13 |
super (context, attrs, defStyle);
|
19 |
public ParentListView(Context context, AttributeSet attrs) {
|
21 |
super (context, attrs);
|
30 |
public boolean onInterceptTouchEvent(MotionEvent ev) {
|
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" >
|
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. */
|
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() {
|
20 |
return Array.getLength(array);
|
24 |
public Object getItem( int position) {
|
26 |
return array[position];
|
30 |
public long getItemId( int position) {
|
36 |
public View getView( int position, View convertView, ViewGroup parent) {
|
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的情况。
后记: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
分享到:
相关推荐
在ListView中嵌套Switch开关,点击ListView条目来切换Switch的状态
ListView嵌套GridView以及ListVIew嵌套ListVIew还有时间轴效果想要了解更多请前往http://blog.csdn.net/u014452224
listView嵌套listViewDemo,解决了listView嵌套listView后,子ListView不能显示全部内容的问题,通过自定义Listview解决
完美解决ListView内部嵌套GridView导致各种异常,无法点击的问题,包含遇到的问题可能有:1、gridview显示不全。无法占满。2、listView点击item点击事件无效 3、GridView点击事件无效。4、假如GridView每行显示3个。...
listview嵌套listview简单的小案例
ListView嵌套ListView并且实现动态设置子ListView的高度,
利用android studio实现listview嵌套listview,技术含量不是特别高,希望能帮助大家,这里就意思一下,给1分算啦
ListView 嵌套ListView 的一个demo,可以实现二级列表。
ListView嵌套ListView,checkbox全选,多选,父item全选后,下面的子item全部选中
直接两个listView 布局文件,数据直接输入,获得 嵌套ListView 的适配效果
水平 横线 listView 嵌套在 ScrollView中 已经处理滑动卡和嵌套在scrollview 中显示的相关问题
listView嵌套listView
viewpage嵌套listview,效果:滑动listview删除,滑动非listview区域viewpage翻页
ListView嵌套ListView时出现的问题1.子ListView展示一条(方法,重写ListViewListViewInScrollView)2.子LsitView的出现是由点击事件触发的,故存在问题:父ListView某一Item点击出现了子ListView,然后往下滑动时,...
代码给你思路,避免无头绪,你懂的
HorizontalListView的应用:Listview中完美嵌套HorizontalListView
实现每个listview的item 就是一个gridview
Android中自定义ListView,解决scrollview嵌套listview 滑动事件冲突,是非常常见的问题,我写了一个例子和大家交流学习
Scrollview中嵌套listview,listview中再嵌套listview代码示例工程