2

在我的 ExpandableListView 中,我希望每个组的第一个子项与组中的其他子项具有不同的 xml 视图。

我想要实现的是这样的:

在此处输入图像描述

我设法用以下代码做到这一点:

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View newView, ViewGroup parent) {



    if (newView==null){

        // CHILD HEADER
        if (childPosition == 0){
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            newView = infalInflater.inflate(R.layout.super_trener_cell_child_header, null);


        // CHILD CELL
        else{
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            newView = infalInflater.inflate(R.layout.super_trener_cell_child, null);
            }
    }

    return newView;
}

这很好用。然而,这是一个问题..每当我扩大和缩小单元格时,我都会得到另一个子单元格的出现顺序。

以下是一些屏幕截图,显示了正在发生的事情(数字代表它们应该出现的顺序):

在此处输入图像描述 在此处输入图像描述

每次单击组标题后,此单元格反转的原因可能是什么?

编辑:整个适配器代码:

公共类 SuperTrenerCellAdapter 扩展 BaseExpandableListAdapter {

Context context;
List<SuperTrenerTopic> topics = new LinkedList<SuperTrenerTopic>();
private static LayoutInflater inflater = null;

public SuperTrenerCellAdapter(List<SuperTrenerTopic> topics, Context context) {
    this.topics = topics;
    this.context = context;
}

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

@Override
public int getChildrenCount(int groupPosition) {
    SuperTrenerTopic topic = topics.get(groupPosition);
    return topic.getLessonList().size()+1;
}

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

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

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.super_trener_cell_header, null);
    }
    SuperTrenerTopic topic = topics.get(groupPosition);
    TextView topicTitle = (TextView)convertView.findViewById(R.id.stc_topicName);
    topicTitle.setText(topic.getName());

    return convertView;
}

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

    SuperTrenerTopic topic = topics.get(groupPosition);
    if (convertView==null){

        // CHILD HEADER
        if (childPosition == 0){
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.super_trener_cell_child_header, null);


        }

        // LESSON CELL
        else{
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.super_trener_cell_child, null);
            SuperTrenerLesson stl = topic.getLessonList().get(childPosition-1);
            TextView title = (TextView)convertView.findViewById(R.id.st_cell_child_title);

            title.setText(""+stl.getTitle());

        }
    }

    return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    if (childPosition == 0){
        return false;
    }
    else{
        return true;
    }
}

}

4

1 回答 1

3

问题是您在 getChildView 中的 convertView。注释掉一分钟的 convertView 空检查,这样你就可以看到发生了什么。

让它看起来像这样:

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

    SuperTrenerTopic topic = topics.get(groupPosition);
    //if (convertView==null)
{

        // CHILD HEADER
        if (childPosition == 0){
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.super_trener_cell_child_header, null);


        }

        // LESSON CELL
        else{
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.super_trener_cell_child, null);
            SuperTrenerLesson stl = topic.getLessonList().get(childPosition-1);
            TextView title = (TextView)convertView.findViewById(R.id.st_cell_child_title);

            title.setText(""+stl.getTitle());

        }
    }

    return convertView;
}

当您打开和关闭标题时,它会回收视图,因此 convertView 可能不会返回为空,因此它会使用存储的旧数据。您没有使用回收视图(convertView)设置任何新内容。您需要更改条件以适应子视图实际被回收的时间(即当 convertView 不会为空时)。

于 2014-10-23T16:06:07.220 回答