简体   繁体   中英

Android ListView embedded within another ListView height not wrapping

I have a ListView with a custom view rendered by an adapter. It contains an image, some text and another ListView. In the getView method of adapter I creator another adapter to set up the embedded ListView.

It all works fine, but for some reason the height of the embedded ListView is only enough to view one list item. If I manually set the height to pixels I can see more items in the list. All heights are set to wrap_content so I'm not sure why it's not working.

My first ListView looks like this:

<ListView
    android:id="@+id/newswire"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:cacheColorHint="#ffffff"
/>

The item that gets rendered for this ListView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:padding="5dp"
  >

      <ImageView
          android:id="@+id/aggSparkAvatar"
          android:layout_width="45dp"
          android:layout_height="45dp"
          android:scaleType="centerCrop"
      />

    <LinearLayout
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    >

        <TextView
              android:id="@+id/aggSparkName"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:textColor="#1B5F7C"
              android:textStyle="bold"
        />

        <TextView
              android:id="@+id/aggSparkDateStamp"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:textColor="#000000"
              android:textStyle="bold"
        />

        <View
            android:layout_height="1dp"
            android:layout_width="fill_parent"
            android:background="#F2F2F2"            
        />

        <ListView
            android:id="@+id/aggSparkList"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:cacheColorHint="#ffffff"
        />

  </LinearLayout>

</LinearLayout>

Then for the embedded ListView each item looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:padding="5dp"
  >

      <ImageView
          android:id="@+id/sparkAvatar"
          android:layout_width="45dp"
          android:layout_height="45dp"
          android:scaleType="centerCrop"
      />

    <LinearLayout
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    >
      <TextView
          android:id="@+id/sparkTitle"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textColor="#000000"
          android:textStyle="bold"
      />

      <TextView
          android:id="@+id/sparkText"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textColor="#000000"
      />

      <TextView
          android:id="@+id/sparkDateStamp"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textColor="#A2A2A2"
      />

  </LinearLayout>

</LinearLayout>

it's never a good practice to include scrollable views inside another scrollable view. you should consider redesigning your application.

in your case, the ListView you're embedding is set to wrap_content, wrap_content - also not a recommended practice. a ListView is designed to show a list of items that collectively will be too big for the screen to show at once. using wrap_content, wrap_content doesn't make much sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM