标签:
众所周知ListView与ScrollView都具有滚动能力,对于这样的View控件,当ScrollView与ListView相互嵌套会成为一种问题:
问题一:ScrollView与ListView嵌套导致ListView显示不全面
问题二:ScrollView不能正常滑动
解决方式一:
ScrollView+LinearLayout+ListView可以换成ScrollView+LinearLayout+LinearLayout,对于开发中,ScrollView所能滚动的样式形式各异,另外的话,ScrollView所显示的内容肯定不会太多,因此这种方案是合理而且可选的
解决方式二:
同样是替换:ListView具有HeaderView与FooterView2部分,因此,在非下拉刷新,上拉加载的需求中,完全可以使用ListView来代替ScrollView,因此是合理可选的方案
<ScrollView
android:id="@+id/ScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffffff"
android:scrollbars="vertical" >
<LinearLayout
android:id="@+id/tmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本备忘"
android:textSize="28sp" />
<ListView
android:id="@+id/listview0"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="智能备忘"
android:textSize="28sp" />
<ListView
android:id="@+id/listview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
private void initView() {
textListView = (ListView) findViewById(R.id.listview0);
intelListView = (ListView) findViewById(R.id.listview1);
textListView.setAdapter(new MyTestAdapter(this));
intelListView.setAdapter(new MyTestAdapter(this));
new ListViewUtils().setListViewHeightBasedOnChildren(textListView);
new ListViewUtils().setListViewHeightBasedOnChildren(intelListView);
}
解决方式三:
通过给ListView设置LayoutParams属性来改变。
主动计算和设置ListView的高度,这样的结果最终得出类似解决方案一效果,具体来说缺点是大材小用,但也是合理的解决办法。
public class ListViewUtils {
public void setListViewHeightBasedOnChildren(ListView listView) {
// 获取ListView对应的Adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0); // 计算子项View 的宽高
totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
listView.setLayoutParams(params);
}
}
标签:
原文地址:http://www.cnblogs.com/dubo-/p/5573050.html