嵌套Listview仅显示第一项

时间:2018-03-28 13:53:03

标签: android listview

我的tablayout中有一个显示列表视图的片段:

<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,其中exampleArray为items:

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

我的问题是,只显示了这个内部列表视图的第一个元素。外部Listview也不再可点击了。有谁知道为什么会这样?我做错了吗?

2 个答案:

答案 0 :(得分:0)

<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>
  

请将'match_parent'与'ListView'一起使用,否则请使用RecyclerView

答案 1 :(得分:0)

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

如果您没有任何其他选项,则必须测量内部ListView高度。这样,所有项目都会同时膨胀,您无法利用recycle的{​​{1}}属性。

要测量内部ListView,请使用此方法

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属性。

还有其他问题,您也可以查看,

检查thisthis