Caxa
Caxa

Reputation: 19

Expandable listview item height with another listview inside

I have an ExpandableListView, where every group has one child. Every child is another ListView with different number of rows. There is a problem with ExpandableListView item height in expanded condition, it's not wraping to child ListView content. I just can set a concrete height value in child listview layout. But i want to get an ExpandableListView automatically wrap every child. Is it possible?

getChildView() from my ExpandableListAdapter:

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    if( convertView == null ) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        convertView = inflater.inflate( R.layout.anit_object_data, parent, false );
    }
    ListView lv = (ListView)convertView.findViewById( R.id.listData );
    List<ListData> valuesList = getItems().get(groupPosition).getListData( getContext() );
    ListDataAdapter adapter = new ListDataAdapter( getContext(), android.R.layout.simple_list_item_1, valuesList );
    lv.setAdapter(adapter);

    return convertView;
}


XML for child ListView:

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

    <LinearLayout
        android:id = "@+id/listHolder"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ListView
            android:id="@+id/listData"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:scrollbars="none" >
        </ListView>

    </LinearLayout>

</LinearLayout>



I was trying to measure child ListView height in getChildView() of adapter, but ExpandableListView expanded item height is still incorrect.

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    if( convertView == null ) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        convertView = inflater.inflate( R.layout.anit_object_data, parent, false );
    }
    ListView lv = (ListView)convertView.findViewById( R.id.listData );
    List<ListData> valuesList = getItems().get(groupPosition).getListData( getContext() );
    ListDataAdapter adapter = new ListDataAdapter( getContext(), android.R.layout.simple_list_item_1, valuesList );
    lv.setAdapter(adapter);

    lv.measure( MeasureSpec.EXACTLY, MeasureSpec.UNSPECIFIED );
    convertView.getLayoutParams().height = lv.getMeasuredHeight();

    return convertView;
}

Upvotes: 2

Views: 3860

Answers (2)

gmendes
gmendes

Reputation: 117

You dont need a listview inside the expandablelist, its control yourself the inflate of childs positions, all you have be is pass other list and take the childposition

Upvotes: 1

evan
evan

Reputation: 318

well,I have tried your mind that the group child view was a listview. But the result was not good. Because the height of the childview(listview) was hard to measure.So I changed the solution as the normal way: the child view was just an item that is just like a listview's item. So that we override the getChildView() method as well.

Upvotes: 1

Related Questions