大多数时候,我们用ListView来加载数据的页面不需要在ListView的外面再套上一个ScollView,因为ListView本身可以滚动显示数据。有时我们页面中除要用ListView显示列表数据之外还要显示其它数据,这时候就需要在整个页面最个层套上一个Scollview,否则显示就可能出现问题(比如在ListView上面已经有很多其它数据,显示在手机上直接导致ListView看不见了,这时就要在整个屏幕布局加ScollView实现滑动界面),用过ScollView嵌套ListView的朋友都知道,在不做任务处理的情况下,ListView的数据只能显示一行多一点点,其它的就看不到了,这个问题怎么解决呢?通常我们可以有下面两种方式:
1,计算高度:
private inttotalHeight=0;
/*得到适配器*/
Aadpter adapter= listView.getAdapter();
/*遍历控件*/
.getCount();i++){
/*测量一下子控件的高度*/
}
/*2、赋值给ListView的LayoutParams对象*/
ViewGroup.LayoutParamsparams=listView.getLayoutParams();
listView.setLayoutParams(params);
}
2,,重写ListView的onMeasure:
public class WholeListView extends ListView { public WholeListView(Context context) { super(context); } public WholeListView(Context context, AttributeSet attrs) { super(context, attrs); } public WholeListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
当然上面两个方法对GridView及Expandlistview也是有效的。
其实你已经错过了旅行的意义。