Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpandableList getChildView running twice per child

Tags:

android

I've got and ExpandableList which I assign to an extended BaseExpandableListAdapter. In here, I implement the getChildView method to setup and return a View for each child:

public View getChildView(int groupIndex, int childIndex, boolean isLastChild, View convertView, ViewGroup parent) {

    LinearLayout layout;
    if (convertView == null) {
            layout = (LinearLayout) LayoutInflater.from(MyApp.getContext()).inflate(R.layout.rooms_room_list_row, parent, false);
        } else {
            layout = (LinearLayout) convertView; 
        }

    // Do some custom stuff with views on the layout
    ...

    return layout;

}

While debugging, I've noticed that getChildView is executed twice per child. All the values passed in (i.e, groupIndex, childIndex, isLastChild) are the same... so in group 3, if I've got two children, I'll see:

groupIndex = 3, childIndex = 0

then

groupIndex = 3, childIndex = 1

then it repeats:

groupIndex = 3, childIndex = 0

and finally:

groupIndex = 3, childIndex = 1

My view appears fine, i.e, there are only two children listed for the group, but why is it doing all the extra work?

UPDATE & ANSWER

It seems that if the the listview is set to a height of wrap_content then the getChildView will be called twice per child. Changing the height to fill_parent seems to fix this behavior.

like image 440
bugfixr Avatar asked Dec 09 '22 07:12

bugfixr


1 Answers

As I mentioned in your other question, the ListView calls getView() to get the dimensions (width and height) of some items first, then calls the getView() again to actually render the items. Check this video out, it's "required reading" for Android. The bit that deals with your question is at minute 41:30.

like image 68
dmon Avatar answered Dec 29 '22 03:12

dmon