<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"> <ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" > </ListView> </ScrollView>
这里面只有一个ListView为何要在外面嵌套一个ScroolView呢?
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"> <ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" > </ListView> </ScrollView>
这里面只有一个ListView为何要在外面嵌套一个ScroolView呢?
/**
* 设置列表框高度,解决滚屏问题
* @param listView 列表对象
* @param addHeight 增加的调节高度,默认是0
*/
public static void initListViewHeight(ListView listView,int addHeight) {
Adapter adapter = listView.getAdapter();
if (adapter == null || adapter.getCount() == 0) {
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = 0;
listView.setLayoutParams(params);
return;
}
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (adapter.getCount() - 1));
params.height += 20 + addHeight;
listView.setLayoutParams(params);
}