【发布时间】:2016-03-12 03:13:37
【问题描述】:
我有一个ScrollView,里面有一个ListView,后面跟着几个TextView。我使用的ListView 是使用自定义适配器填充的,项目的宽度可能会根据我们所在的页面而有所不同。这是我的ScrollView:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/options_listview">
</ListView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/options_listview">
<TextView
android:layout_width="match_parent"
android:layout_height="24dp"
android:text="Explanation:"
android:id="@+id/explanation_header"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/explanation_text"
android:layout_below="@+id/explanation_header"
android:text="stuff..."/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
问题
ListView 的高度未完全显示。我以编程方式将高度设置为:
public static void setListViewHeight(ListView list) {
int height = 0;
for (int i = 0; i < list.getCount(); i++) {
View childView = list.getAdapter().getView(i, null, list);
childView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
height+= childView.getMeasuredHeight();
}
//dividers height
height += list.getDividerHeight() * list.getCount();
ViewGroup.LayoutParams params = list.getLayoutParams();
params.height = height;
list.setLayoutParams(params);
Log.i("Height ", height + "");
}
现在我在日志中看到,无论项目高度如何,所有 listViews 的高度都相同。如何使 ListView 在我的滚动视图中完全可见?
【问题讨论】:
-
更改列表视图高度匹配父项
-
列表视图是否滚动?意味着有足够的对象列表视图可以滚动或覆盖整个空间?
-
不,列表视图没有滚动。
ListView+TextView被放在ScrollView中,共同滚动,而不是单独滚动。 -
是的,这就是问题所在。两个滚动视图没有一起滚动。 ListView 也有滚动视图属性,即滚动。你为什么使用滚动视图?
-
我有一个列表视图和下面的几个文本视图。所有这些都无法包含在屏幕中。所以,我把它们都放在了一个 ScrollView 中。
标签: android listview scrollview