GetChildView method is run 2 times in the expandable ListView

I am creating an application that contains an extensible list view. children's performances are created dynamically. and it works successfully. While debugging, I found that the getChildview function works 2 times.

I create dynamic layouts and put them in a list. when getChildView is run 2 times, layouts are added 2 times to the list.

+3


source to share


5 answers


getChildView()

not suitable for making children. It could be called quite often. Anyway, the rendering process has to visit children twice.



There is no way to judge where is the right place to add children to your list, or even if your approach to the list is the right way to do it, without further information.

+1


source


If you are regenerating the list by pushing a group, then deleting it may be the solution. For example, in the following code, getChildView () is always called twice because of myList.expandGroup (groupPosition).



public boolean onGroupClick(ExpandableListView parent, View v,
            int groupPosition, long id) {

        //get the group header
        HeaderInfo headerInfo = medicationDate.get(groupPosition);
        myList.expandGroup(groupPosition);
        //set the current group to be selected so that it becomes visible
        //myList.setSelectedGroup(groupPosition);
        //display it or do something with it
        Toast.makeText(getBaseContext(), "Child on Header " + headerInfo.getHeaderInfo()+"with childsize"+headerInfo.getChildInfo().size(), 
                Toast.LENGTH_SHORT).show(); 

        return false;
    }

      

0


source


I am new to android development and may be wrong, but as I see it getChildView()

has a 4th argument View convertView

which is zero first time view. Once created, it is saved and used again when needed. Therefore, if you create new views in getChildView()

, it is enough to have something like this

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
  if (convertView != null) {
      // View is already created here, update it if you like
      return convertView;
  }
  // Else create your view(s) here and return the root of view container as usual
  ...
  return convertView; // or whatever your root view is
}

      

0


source


List height should be match_parent

instead of wrap_content

.

0


source


one thing worked for me.

@Override
public boolean hasStableIds() {
    // To avoid refreshing return true and makesure Ids each position have same view.
    return true;
    //return false;
}

      

0


source







All Articles