转载自:http://www.it165.net/pro/html/201303/5009.html
按照android的标准,ScrollView中是不能嵌套具有滑动特性的View的,但是有时如果设计真的有这样做的需要,或者为了更方便简单的实现外观(比如在外在的大布局需要有滑动的特性,并且内部有类似于List的UI结构,那么ListView + Adpater的方式来实现里面的效果就很方便,算是违规抄近道的一种方式吧),有时就会不得不采用这种怪异的组合方式。
先说下这种方式如果不做特殊处理时会出现的冲突和问题:
1,在SrollView中嵌套ListView,ListView的显示会有问题,只显示一行或显示效果与预期不同,这是因为android禁止这样使用,放入ScrollView中的ListView的高度是无法计 算的。
2,嵌套中的子ListView和GridvIew是无法滑动的,因为子控件的滑动事件会被外面的ScrollView吃掉,如果想让子控件可以滑动,只能强行的截取滑动的相关事件了。
言归正传,嵌套的解决方案:
1,第一种方案,也是我比较推荐的方案,就是重写ListView与GridView,让其失去滑动特性:
01.
package
com.perfect.xiaoao.all.ui;
02.
03.
import
android.content.Context;
04.
import
android.util.AttributeSet;
05.
import
android.widget.GridView;
06.
07.
/**
08.
* Created by IntelliJ IDEA.
09.
* User: zhUser
10.
* Date: 13-1-24
11.
* Time: 下午6:53
12.
*/
13.
public
class
NoScrollGridView
extends
GridView{
14.
15.
public
NoScrollGridView(Context context, AttributeSet attrs){
16.
super
(context, attrs);
17.
}
18.
19.
public
void
onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec){
20.
int
mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >>
2
, MeasureSpec.AT_MOST);
21.
super
.onMeasure(widthMeasureSpec, mExpandSpec);
22.
}
23.
}
24.
25.
package
com.perfect.xiaoao.all.ui;
26.
27.
import
android.content.Context;
28.
import
android.util.AttributeSet;
29.
import
android.widget.ListView;
30.
31.
/**
32.
* Created by IntelliJ IDEA.
33.
* User: zhUser
34.
* Date: 13-1-24
35.
* Time: 下午6:53
36.
*/
37.
public
class
NoScrollListView
extends
ListView{
38.
39.
public
NoScrollListView(Context context, AttributeSet attrs){
40.
super
(context, attrs);
41.
}
42.
43.
public
void
onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec){
44.
int
mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >>
2
, MeasureSpec.AT_MOST);
45.
super
.onMeasure(widthMeasureSpec, mExpandSpec);
46.
}
47.
}
2,第二种方案,也是网上流行的一种解决办法,人工计算子控件的尺寸,解决办法:
在listview.setAdapter()之后调用Utility.setListViewHeightBasedOnChilren(listview)就Okay 了。
01.
public
class
Utility {
02.
public
static
void
setListViewHeightBasedOnChildren(ListView listView) {
03.
//获取ListView对应的Adapter
04.
ListAdapter listAdapter = listView.getAdapter();
05.
if
(listAdapter ==
null
) {
06.
// pre-condition
07.
return
;
08.
}
09.
10.
int
totalHeight =
0
;
11.
for
(
int
i =
0
, len = listAdapter.getCount(); i < len; i++) {
//listAdapter.getCount()返回数据项的数目
12.
View listItem = listAdapter.getView(i,
null
, listView);
13.
listItem.measure(
0
,
0
);
//计算子项View 的宽高 www.it165.net
14.
totalHeight += listItem.getMeasuredHeight();
//统计所有子项的总高度
15.
}
16.
17.
ViewGroup.LayoutParams params = listView.getLayoutParams();
18.
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() -
1
));
19.
//listView.getDividerHeight()获取子项间分隔符占用的高度
20.
//params.height最后得到整个ListView完整显示需要的高度
21.
listView.setLayoutParams(params);
22.
}
23.
}
原理就是:
设置完ListView的Adapter后,根据ListView的子项目重新计算ListView的高度,然后把高度再作为LayoutParams设置给ListView,这样它的高度就正确了,通过人工算取控件的应有高度,再设置给ListView
注意:这个方案中子ListView的每个Item必须是LinearLayout,不能是其他的,因为其他的Layout(如RelativeLayout)没有重写onMeasure(),所以会在onMeasure()时抛出异常。
最后,建议大家还是少用这样的设计,毕竟这种方式是不标准与不规范的。