0
我知道将两个ListViews放在ScrollView中并不是最佳做法。然而,这是我能想到的最好的解决方案:ScrollView中的两个ListViews
我想要两个列表(第一个包含1-5个项目,最后一个包含最多20个项目)在可滚动的下方查看,每个都有自己的标题。 ListViews不应该自己滚动,他们应该改变高度来包装他们的内容。可滚动部分将由ScrollView处理。
因为ListView控件不支持这个本身我使用下面的代码:
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
Log.d("DEBUG", "TotalHeight:" + totalHeight);
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listAdapter.getCount() * listView.getDividerHeight());
listView.setLayoutParams(params);
listView.requestLayout();
}
然而,这使得列表视图约10倍大,他们应该是。如果我搜索我遇到的问题,我总是会找到上述解决方案,但对我来说,这似乎并不奏效。
有没有办法解决我的代码或有没有更好的方法去呢?
XML:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.miscoriadev.svvirgoapp.fragments.frag_activiteiten"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_acti_komende_activiteiten"
android:textSize="32sp"
android:text="Komende activiteiten"
android:textColor="@color/TextColorDark"
android:gravity="center"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_acti_komende_activiteiten"
android:id="@+id/lv_komende_activiteiten">
</ListView>
<TextView
android:paddingTop="@dimen/activity_vertical_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="Afgelopen activiteiten"
android:textColor="@color/TextColorDark"
android:gravity="center"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:id="@+id/tv_acti_afgelopen_activiteiten"
android:layout_below="@id/lv_komende_activiteiten"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_acti_afgelopen_activiteiten"
android:id="@+id/lv_afgelopen_activiteiten">
</ListView>
</RelativeLayout>
您需要两个列表同时滚动并显示在同一屏幕上吗?如果是这样,我会推荐使用[GridView](https://developer.android.com/guide/topics/ui/layout/gridview.html)和2列。如果没有,我会建议使用[ViewPager](https://developer.android.com/training/animation/screen-slide.html)与[TabLayout](https://developer.android.com/reference/android /support/design/widget/TabLayout.html)。这里是一个教程如何做到这一点:https://guides.codepath.com/android/google-play-style-tabs-using-tablayout – klauskpm
不,我只是想让他们在彼此之下,并有两个列表一个可滚动的框架 –
我相信如果您使用水平的“ListView”而不是垂直的ListView,则会更方便。我甚至建议使用[RecyclerView](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html),可以是水平或垂直。 – MohanadMohie