android - Listview inside listview and issue with height of the nested listview -


i have 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 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 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 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 child listview displays 1 item(first item), 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 manually adjust height of child listview say, 250dp, displays more items want height of child listview auto wrap according number of items. how can achieve this? (i have adapters both listview i'm not sure if it's relevant discussion , hence i'm not including it).

as general rule of thumb should never have listview embedded in listview. android isn't fond of embedding scrollable content.

if wan't describing. can use (embed) listview aggressively pre-computes it's height based on contents of adapter 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);       (int = 0; < 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 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> 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -