getGroupView扩展列表为每个组点击多次调用android

时间:2018-02-28 12:15:01

标签: android navbar expandablelistview

我有两组及其各自的项目。 当我点击第一组时,它会扩展&然后点击它我点击第二组,两者都显示基于组位置的正确数据。

但当我关闭第一组时,第二组中的数据发生了变化,我的意思是我的组位置错误,即第一组中的数据为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

2 个答案:

答案 0 :(得分:1)

您的getChildView方法错误...目前,只有在视图为空时才设置正确的文本。但是,有些视图会被重复使用......因此,您始终需要更新文本。

按如下方式更新:

@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;
}

答案 1 :(得分:0)

请确保在您的BaseExpandableListAdapter中使用了覆盖hasStableIds()的方法,该方法返回true ,而不是 false ,如下所示。

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