ExpandableListView的组视图膨胀错误

时间:2014-04-23 10:55:48

标签: android android-layout expandablelistview

我正在项目中实施ExpandableListView,我遇到以下问题。

我在Type expandablelistview adapter extends提供的列表中有一个名为BaseExpandableListAdapter的变量。基于type的值(仅限1,2或3),我正在对3种不同布局中的一种进行充气。我的getGroupView()就是这样,

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

    LayoutInflater inflater = (LayoutInflater) context.getSystemService
(Activity.LAYOUT_INFLATER_SERVICE);

    final int type =  ((cast) getGroup(groupPosition)).getType();

    if(convertView == null){
        if(type == 1){
            convertView = inflater.inflate(R.layout.layout1, parent, false);
        }else if(type == 2){
            convertView = inflater.inflate(R.layout.layout2, parent, false);
        }else if(type == 3){
            convertView = inflater.inflate(R.layout.layout3, parent, false);
        }   
    }

    TextView text = (TextView) convertView.findViewById(R.id.tvID);

    text.setText("some text");

    return convertView;
}

所以,当activity最初加载时,一切都很好。但是当我展开第一组时,下面组中的layout也会被改变。展开第二组,组3,4,5的layout,...(但不是1)被改变,依此类推。当我崩溃一切都很好。

这里似乎有什么问题?我错过了什么吗?我的做法是对的吗?

此外,由于我正在膨胀3个不同的layout,我如何在这些布局中获得对不同元素(imageview,textview,...)的引用,以便我可以使用所需的值填充它们?

编辑:完整适配器实施

public class EventExpandableListViewAdapter extends BaseExpandableListAdapter{

/* Class1 {
    var1;
    var2;
    ...
    ArrayList<Class2> var3;
}
*/

Context context;
final List<Class1> events;

public EventExpandableListViewAdapter(Context context, ArrayList<Class1> events) {
    this.context = context;
    this.events = events;
}


@Override
public Object getChild(int groupPosition, int childPosition) {

    return events.get(groupPosition).getVar3().get(childPosition);
}

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

    return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if(convertView == null){

        convertView = inflater.inflate(R.layout.childlayout, parent, false);
    }

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return events.get(groupPosition).getVar3().size();
}

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

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

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

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

    LayoutInflater inflater = (LayoutInflater) context.getSystemService
(Activity.LAYOUT_INFLATER_SERVICE);

    final int type =  ((cast) getGroup(groupPosition)).getType();

    if(convertView == null){
        if(type == 1){
            convertView = inflater.inflate(R.layout.layout1, parent, false);
        }else if(type == 2){
            convertView = inflater.inflate(R.layout.layout2, parent, false);
        }else if(type == 3){
            convertView = inflater.inflate(R.layout.layout3, parent, false);
        }   
    }

    TextView text = (TextView) convertView.findViewById(R.id.tvID);

    text.setText("some text");

    return convertView;
}

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

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

}

编辑2:我试图通过仅使用1个布局创建3种不同布局的错觉,并根据类型的值隐藏/显示不同的视图。结果仍然相同。

1 个答案:

答案 0 :(得分:0)

从0开始检查类型的条件:

    if(type == 0){
        convertView = inflater.inflate(R.layout.layout1, parent, false);
    }else if(type == 1){
        convertView = inflater.inflate(R.layout.layout2, parent, false);
    }else if(type == 2){
        convertView = inflater.inflate(R.layout.layout3, parent, false);
    }