2

I am creating a custom ExpandableListView to support three levels using the tutorial found here.

I have the list working and expanding/collapsing as expected.

The issue I am having is that when a user presses an item, android only recognizes a portion of each item as the clickable area and that area is the only portion that highlights when selected.

If you run the tutorial code, or look at the image below, the Second Level and Third Level backgrounds should take up the full width, just as First Level does.

multilevel expandablelistview

I have tried setting the LayoutParams on my TextView called tv like this:

tv.setLayoutParams(new ListView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

I want background and highlighting to take up the full width of the view on every level.

All of my code follows the tutorial, but I can post specific pieces if it would help.

EDIT: Added Image

mguida
  • 858
  • 1
  • 10
  • 19
  • I have found a solution to this and will post when SO allows me too (I don't have enough reputation to answer my own question right now) – mguida Jul 21 '14 at 20:43

1 Answers1

2

I found a solution to this issue if anyone also needs it.

In the CustExpListView class you need to override the onMeasure method.

The tutorial shows to do so in this way:

@Override    
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

However this will only use as much of the width as necessary to fit the text, thus not highlighting the full bar. To solve this issue change the onMeasure method to this:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    widthMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY);
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

This always assigns the width to that of widthMeasureSpec, which if you have set the LayoutParams to MATCH_PARENT will fill your view. The height uses Integer.MAX_VALUE so that the second expandable list may use as much space as necessary for its children.

mguida
  • 858
  • 1
  • 10
  • 19