带有ExpandableListView的BaseExpandableListAdapter返回错误的子视图

时间:2015-02-18 00:07:33

标签: android expandablelistview expandablelistadapter

我有使用ExpandableListView的BaseExpandableListAdapter,但我无法将我的孩子ViewHolder初始化为正确的视图。我在getChildView中设置了一个断点,并且第一次调用它时一切都很好

groupPosition:0,childPosition:0,convertView:null

ViewHolder得到设置,我们对第一行(0)有好处。下次它被调用我得到:

groupPosition:0,childPosition:1,convertView:来自行(0)的ViewHolder对象

为什么从第0行返回视图?我已经包含了我从BaseExpandableListAdapter覆盖的方法。是否有其他方法需要覆盖才能获得正确的视图?

child_layout.xml
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="9">
    <LinearLayout android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal"
                  android:layout_weight="1">
        <EditText.../>
    </LinearLayout>
    ...
</LinearLayout>




// Group and children are both collections
@Override
public int getGroupCount() {
    return group.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    return group.get(groupPosition).getChildren().size();
}

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

@Override
public Object getChild(int groupPosition, int childPosition) {
    return group.get(groupPosition).getChildren().toArray()[childPosition];
}

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

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

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

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

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

    final GroupViewHolder groupViewHolder;
    if( null == convertView ) {
        convertView = LayoutInflater.from( context ).inflate(R.layout.group_layout, parent, false);
        groupViewHolder = new GroupViewHolder();
        // add delete group button
    } else {
        groupViewHolder = (GroupViewHolder) convertView.getTag();
    }
    return convertView;
}

@Override
public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    View v = convertView;
    final ViewHolder viewHolder;
    if (null == v){
        v = LayoutInflater.from( context ).inflate(child_layout, parent, false);
        viewHolder = new ViewHolder();
        // bind edittext fields
        v.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) v.getTag();
    }
    return v;
}

1 个答案:

答案 0 :(得分:1)

我的页面布局包含一个包装所有内容的垂直LinearLayout。我有layout_height =&#34; wrap_content&#34;。将此更改为match_parent修复了该问题。希望这有助于某人。