0

I'm developing an Android app with a left slide menu contains some LinearLayout and a ExpandableListview in the middle like this image

enter image description here

The child view of ExpandableListview contains a TextView and a ListView below, ListView's visibility is gone in default. When click on the TextView, ListView will be visible.

The problem is that when click on the parent view of ExpandableListview, the list's height also expand to wrap it's content. But when click on child view (also TextView), the list'height doesn't change and I have to scroll down to see the last item.

To set height of ExpandableListview inside ScrollView, I used this code:

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        if(listItem instanceof SecondLevelExpandableListView)
            Log.e("abc", "SecondLevelExpandableListView");
        if (listItem instanceof ViewGroup) {
            Log.e("abc", "ViewGroup");
            listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        }
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

Screenshot of problem

enter image description here

Please help me to solve this problem, I need the list always to wrap it's content.

Thanks in advance.

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Glenn
  • 71
  • 3
  • 9

2 Answers2

0

when you expand animation ends, call this:

listView.smoothScrollTo(x,y);

you can find the y with listView.getScrollY - maxY on Screen Seen

Eren Utku
  • 1,731
  • 1
  • 18
  • 27
  • No. I mean that the listview always display all item and don't need to scroll. – Glenn May 12 '16 at 09:26
  • So can you try again what is your problem? – Eren Utku May 12 '16 at 09:29
  • My problem is that when I click on the Texview to visible the list below, the height of ExpanableListview doesn't extend to wrap it content. I want the ExpandableListview's height also extend like when I click on it's parent view. – Glenn May 12 '16 at 09:38
  • I updated the screenshot on the question, the listview's height doesn't extend and the content is hided – Glenn May 12 '16 at 09:44
  • here you can read this: http://stackoverflow.com/questions/6071131/how-to-change-the-height-of-the-listview-dynamically-in-andrtoid – Eren Utku May 12 '16 at 09:46
  • as you can see on my question, I also use the dynamic set height for listview but its not works with the child view of ExpandableListview – Glenn May 12 '16 at 09:49
0
Use the Snippet for ExpandableListView

    import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

public class ExpandableHeightListView extends ExpandableListView {

    boolean expanded = false;

    public ExpandableHeightListView(Context context) {
        super(context);
    }

    public ExpandableHeightListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ExpandableHeightListView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    public boolean isExpanded() {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // HACK! TAKE THAT ANDROID!
        if (isExpanded()) {
            // Calculate entire height by providing a very large height hint.
            // But do not use the highest 2 bits of this integer; those are
            // reserved for the MeasureSpec mode.
            int expandSpec = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded) {
        this.expanded = expanded;
    }
}



****
use <classpath for ExpandableHeightListView 
attribs
/>

*****
ExpandableHeightListView expListView = (ExpandableHeightListView)findViewById(id);
Amit Soni
  • 39
  • 6