0

I got a fragment in my tablayout displaying a 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>

This Listview has items which have the following layout:

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

These items all have also a listview which has the exampleArray as items:

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

My Problem is, that only the first element of this inner listview is shown. Also the outer Listview is not clickable anymore. Does anyone know why this is the case? Am I doing anything wrong?

progNewbie
  • 4,362
  • 9
  • 48
  • 107
  • You shouldn't use a scroll-container (ScrollView, ListView...) element inside another scroll-container element with the same orientation.. And if you have to, use a fixed height on it, not `wrap_content` – HedeH Mar 28 '18 at 13:57
  • @HedShafran I don't want my inner Listview to be scrollable, but the height will be set dynamically so I can't set it fixed. – progNewbie Mar 28 '18 at 14:01
  • Yes you can do it dynamically.. You can measure the item height and multiply it by the items count, then set it on the ListView. (Don't forget to include deviders height if you have them...) – HedeH Mar 28 '18 at 14:02

2 Answers2

1

Its not a good idea to put a scrollable view inside another scrollable view. Try to solve your problem using getItemViewType method.

If you don't have any other option, then you have to measure inner ListView height. This way all item will be inflated at the same time, you can't take advantage of recycle property of ListView.

To measure inner ListView use this method

/**** 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();
} 

Usage:

setListViewHeightBasedOnChildren(listView)

This way you inner LisView will lose recycle property.

There is other questions about this, You can check those too,

Check this and this

Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
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>

Please use 'match_parent' with 'ListView' else use RecyclerView

Rajesh
  • 2,618
  • 19
  • 25
  • Thank you for your input, but this didn't fix the issue. Also, it is not this listview which only shows the first item. It is the inner listview which does this. – progNewbie Mar 28 '18 at 14:00
  • Same thing applied on nested listview, You can't use nested listview in listview instead use Expandable listview or use Nested RecyclerView inside recyclerview or refer this link https://stackoverflow.com/questions/18367522/android-list-view-inside-a-scroll-view – Rajesh Mar 28 '18 at 14:03