getGroupView expandedlist android 每次点击组都会被调用多次

getGroupView expandedlist android getting called multiple times for each tap on group

我有 2 个组和它们各自的项目。 当我点击第一组时,它会展开,然后跟随它我点击第二组,两者都会根据组位置显示正确的数据。

但是当我关闭第一组时,第二组中的数据发生了变化,我的意思是我得到了错误的组位置,即第一组为 0。

此外,如果我直接展开第二组,由于组位置错误,它会显示不正确的数据。

我通过日志观察,每次我点击任何组时,getgroupview 方法都会为每个组调用两次,也分别为子调用。

public class NavAdapter extends BaseExpandableListAdapter {



private Context _context;
private List<String> _listDataHeader;
private List<String> _listDataHeaderCaption;
private HashMap<String, List<String>> _listDataChild;

public NavAdapter(Context context, List<String> listDataHeader,List<String> listDataHeaderCaption,HashMap<String, List<String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataHeaderCaption = listDataHeaderCaption;
    this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {


                    Log.d(AppConstants.MyLogs,"0 : "+headerTitle);
                    LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = infalInflater.inflate(R.layout.list_item, null);
                    TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
                    txtListChild.setText(childText);
       }
    }

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
   return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
    return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public void onGroupCollapsed(int groupPosition) {
    super.onGroupCollapsed(groupPosition);
}

@Override
public void onGroupExpanded(int groupPosition) {

    super.onGroupExpanded(groupPosition);
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {

    String headerTitle = (String) _listDataHeader.get(groupPosition);
    String headerCaptionTitle = (String) _listDataHeaderCaption.get(groupPosition);


    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group, null);
    }

    TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
    lblListHeader.setText(headerTitle);

    TextView lblListHeaderCaption = (TextView) convertView.findViewById(R.id.lblListHeaderCaption);
    lblListHeaderCaption.setText(headerCaptionTitle);

    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}       

CORRECT ORDER WHEN BOTH GROUPS OPEN WRONG - WHEN SECOND GROUP IS OPEN

您的 getChildView 方法有误...目前,您仅在视图为空时才设置正确的文本。但是,有些观点是re-used...所以,你总是要更新文本。

更新如下:

@Override
public View getChildView(int groupPosition, final int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
    final String childText = (String) getChild(groupPosition, childPosition);

    Log.d(AppConstants.MyLogs,"Position: " + position + ": Text: " + childText);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

    TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
    if(txtListChild != null) {
        txtListChild.setText(childText);
    }
    return convertView;
}

确保在您的 BaseExpandableListAdapter 中您有覆盖 hasStableIds() 方法,其中 returns true 而不是 false 如下。

@Override
public boolean hasStableIds() {
    return true;
}