ExpandableListView重新组织每个组展开的子视图顺序

时间:2014-10-23 14:09:05

标签: android expandablelistview childviews

在我的ExpandableListView中,我想让每个组的第一个孩子成为与该组中其他孩子不同的xml视图。

我想要达到的目标是:

enter image description here

我设法使用以下代码执行此操作:

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

这很好用。 然而,这是一个问题。每当我扩张和缩小细胞时,我得到了另一个儿童细胞外观的顺序。

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

enter image description here enter image description here

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

修改: 整个适配器代码:

public class 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;
    }
}

}

1 个答案:

答案 0 :(得分:2)

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

让它看起来像这样:

    @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可能不会返回为null,因此使用存储的旧数据。您没有使用循环视图(convertView)设置任何新内容。当childView实际被回收时(当convertView不为null时),您需要更改条件以适应。