I have this parent layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center|top"
android:text="Title"
android:textSize="20sp" />
<ListView
android:id="@+id/parent_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
and parent_listview's list items have this layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:background="@color/mediumBlue" >
<TextView
android:id="@+id/sub_title"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/child_listview"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:layout_below="@id/sub_title" >
</ListView>
</RelativeLayout>
and child_listview's items have this layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:background="@color/green" >
<TextView
android:id="@+id/childName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp" />
<TextView
android:id="@+id/childDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp" >
</TextView>
</RelativeLayout>
I expect to see
Parent Layout
- Parent ListView
- Parent Item 1
-Child item 1
-Child item 2
-Child item 3
- Parent Item 2
-Child item 1
-Child item 2
- Parent Item 3
-Child item 1
- Parent Item 4
-Child item 1
-Child item 2
-Child item 3
but the child listview displays only one item(first item), something like this
Parent Layout
- Parent ListView
- Parent Item 1
-Child item 1
- Parent Item 2
-Child item 1
- Parent Item 3
-Child item 1
- Parent Item 4
-Child item 1
if I manually adjust the height of child ListView to say, 250dp, it displays more items but I want the height of the child listview to auto wrap according to the number of items. How can I achieve this? (I have adapters for both listview but I'm not sure if it's relevant for this discussion and hence I'm not including it).
As a general rule of thumb you should never have a ListView embedded in another ListView. Android isn't fond of embedding scrollable content.
If you really wan't to do what you are describing. You can use (embed) a ListView that aggressively pre-computes it's height based on the contents of the adapter like so:
public class ExpandedListView extends ListView {
public ExpandedListView(Context context) {
super(context);
}
public ExpandedListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
setListViewHeightBasedOnChildren(this);
}
public static void setListViewHeightBasedOnChildren(ListView listView) {
Adapter listAdapter = listView.getAdapter();
if (listAdapter == null)
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);
if (listItem != null) {
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}
and parent_listview's list items have this layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:background="@color/mediumBlue" >
<TextView
android:id="@+id/sub_title"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_height="wrap_content" />
<com.example.ExpandedListView
android:id="@+id/child_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/sub_title" >
</com.example.ExpandedListView>
</RelativeLayout>
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.