I am making a menu which includes a custom ExpandableListView adapter. Despite trying to match my code as close to the API examples and any other examples I\'ve seen online
Bummer that no one was able to answer this...but, luckly I was able find a solution for this problem so others out there can add expandable list views to their fragments too.
I found the solution while checking out the source code for the spinner (because I wanted to make a spinner with custom entries). The key point in the code was in it's "onMeasure" when it measures all of it's children...well, up to a pre-defined limit of 15, but measuring the children is the main point.
= Solution = When creating your adapter, you need to pass in your parent view. This is key because it's the only link you have to the view from the adapter (only easy way).
public Context context;
public ExpandableListView parent;
private LayoutInflater inflator;
// Only measure this many items to get a decent max height.
private static final int MAX_ITEMS_MEASURED = 15;
public ExpandableFaceList(Context context, final ExpandableListView parent)
{
this.context = context;
this.parent = parent;
this.inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mDensity = context.getResources().getDisplayMetrics().density;
}
Next, in your "getGroupView", because that seems to be the only thing which is commonly called, you'll need to add the following code:
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
if(convertView != null)
{
int nH = 0;
if(isExpanded)
{
nH = measureChildrenHeight(groupPosition);
} else
{
nH = convertView.getMeasuredHeight();
}
parent.getLayoutParams().height = (int) (nH * mDensity);
parent.invalidate();
}
return convertView;
}
The key part in the above code is the "measureChildrenHeight", the "convertView.getMeasuredHeight()" is just to restore the ELV when collapsed. The measure children code is as follows:
int measureChildrenHeight(int groupPosition)
{
int height = 0;
View itemView = null;
LinearLayout viewGroup = new LinearLayout(context);
viewGroup.setOrientation(LinearLayout.VERTICAL);
final int widthMeasureSpec =
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
final int heightMeasureSpec =
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
// Make sure the number of items we'll measure is capped. If it's a huge data set
// with wildly varying sizes, oh well.
int start = 0;//Math.max(0, getSelectedItemPosition());
final int end = Math.min(getChildrenCount(groupPosition), start + MAX_ITEMS_MEASURED);
final int count = end - start;
start = Math.max(0, start - (MAX_ITEMS_MEASURED - count));
for (int i = start; i < end; i++)
{
itemView = getChildView(groupPosition, i, (i+1 == end), itemView, viewGroup);
if (itemView.getLayoutParams() == null)
{
itemView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
itemView.measure(widthMeasureSpec, heightMeasureSpec);
if(i+1 != end)
height += itemView.getMeasuredHeight();//Math.max(height, itemView.getMeasuredHeight());
}
return height;
}
Note: I had to adjust the code to exclude the last item's height because it was adding extra space below the last item. If you have children of different height, there might be trouble. I still get some empty space below the children/ group header, but it's excusable (unlike the previous amount, which was 3-4 children tall).
See if your control is visible. I was setting it to invisible programatically.
The Problem is, your ExpandableListView is in a ScrollView. This is most of the times the result of bad layouts, and you should try to avoid it.
However sometimes it is the only solution (that's actually not true but other solutions might be out of ones scope). Your solution also works but is a bit complex. Instead write a custom ExpandableListView like this:
public class ExpandExpandableListView extends ExpandableListView{
boolean expanded = true;
public ExpandExpandableListView(Context context) {
super(context);
}
public ExpandExpandableListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExpandExpandableListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, 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;
}
}
and in your m_backdrop.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
...
<RelativeLayout
...
<com.package.name.to.class.ExpandExpandableListView
android:id="@+id/FaceList"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/Title_GeneralSettings_text" >
</ExpandableListView>
</RelativeLayout>
</ScrollView>
That's already it. It should work as expected.