嵌套ListView仅显示第一个项目



我在tableOut中显示了一个listView的片段:

<android.support.design.widget.CoordinatorLayout 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"
    android:background="@color/backgroundColor"
    android:fitsSystemWindows="true"
    tools:context=".activities.fragments.OrdersFragment">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ListView
            android:id="@+id/orderListView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@color/colorPrimaryDark"
            android:dividerHeight="1px" />
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

此listView的项目具有以下布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/order_list_order"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp">

    <ImageView
        android:id="@+id/arrowView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_weight="1"
        app:srcCompat="@drawable/ic_downarrow" />
    <LinearLayout
        android:id="@+id/previewLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/order_list_storename"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="Store Name"
            android:textColor="@color/generalText"
            android:textSize="24sp" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            some textview here
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            some textview here
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/extensionLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/previewLayout"
        android:visibility="visible">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Produkte:"
            android:textColor="@color/generalText"
            android:textSize="18sp"/>
        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/exampleArray"/>
    </LinearLayout>
</RelativeLayout>

这些项目都有一个listView,其exkearRay作为项目:

<string-array name="exampleArray">
    <item>Donut</item>
    <item>Pizza</item>
    <item>blabla</item>
</string-array>

我的问题是,仅显示此内部列表视图的第一个元素。另外,外部列表视图不再可单击。有人知道为什么是这种情况吗?我做错了吗?

在另一个可滚动视图中放置可滚动视图不是一个好主意。尝试使用getItemViewType方法来解决问题。

如果您没有其他选择,则必须测量内部ListView高度。这样,所有项目都将同时夸大,您无法利用recycleListView属性。

测量内部ListView使用此方法

/**** Method for Setting the Height of the ListView dynamically. 
 **** Hack to fix the issue of not showing all the items of the ListView 
 **** when placed inside a ScrollView  ****/ 
public static void setListViewHeightBasedOnChildren(ListView listView) {
  ListAdapter listAdapter = listView.getAdapter();
  if (listAdapter == null)
    return; 
  int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
  int totalHeight = 0;
  View view = null;
  for (int i = 0; i < listAdapter.getCount(); i++) {
    view = listAdapter.getView(i, view, listView);
    if (i == 0)
        view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
    view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
    totalHeight += view.getMeasuredHeight();
  } 
  ViewGroup.LayoutParams params = listView.getLayoutParams();
  params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
 listView.setLayoutParams(params);
 listView.requestLayout();
} 

用法:

setListViewHeightBasedOnChildren(listView)

这样,您内部LisView将丢失recycle属性。

还有其他问题,您也可以检查这些问题,

检查此

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">//changed here
    <ListView
        android:id="@+id/orderListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"//and here
        android:divider="@color/colorPrimaryDark"
        android:dividerHeight="1px" />
</RelativeLayout>

请与'listView'一起使用'match_parent'

相关内容

最新更新