编辑
我找到了解决办法,请看下面。
原件:
我创建了一个ExpandableListView,它看起来是这样的:
组0-子0,子1,子2
组1-子0、子1、子2、子3
组2-子0、子1、子2
组3-子0、子1、子2、子3、子4

每个组视图有3~5个子视图,我强制展开所有组视图,

mExpandableListView = (ExpandableListView) mView.findViewById(R.id.expandable_list);
CollectionExpandableListAdapter adapter = new MyExpandableListAdapter(getActivity(), mGroups, mProdChild);
adapter.setInflater(LayoutInflater.from(getActivity().getBaseContext()), getActivity());
mExpandableListView.setAdapter(adapter);
mExpandableListView.setGroupIndicator(null);

for (int i = 0; i < groupList.size() - 1; i++) {
    mExpandableListView.expandGroup(i);
}

另外,下面是我的适配器代码:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {

private ArrayList<String> mGroupItem;
private ArrayList<ProductListData> mTempChild = new ArrayList<ProductListData>();
private ArrayList<Object> mChildItem = new ArrayList<Object>();

private LayoutInflater mInflater;
private Activity mActivity;

private ImageLoader mImageLoader;

public CollectionExpandableListAdapter(Activity context, ArrayList<String> grList,
        ArrayList<Object> childItem) {

    mGroupItem = grList;
    mChildItem = childItem;
    mActivity = context;
    mImageLoader = new ImageLoader(mActivity);
}

public void setInflater(LayoutInflater inflater, Activity activity) {

    mInflater = inflater;
    mActivity = activity;
}

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

    return ((ArrayList<ProductListData>) mChildItem.get(groupPosition)).get(childPosition);
}

@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) {

    mTempChild = (ArrayList<ProductListData>) mChildItem.get(groupPosition);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.adapter_collection_row_child, null);
    }
    ...

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return ((ArrayList<ProductListData>) mChildItem.get(groupPosition)).size();
}

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

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

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

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

    Log.d("LOG", "Group position : " + groupPosition);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.group_row_layout, null);
    }
    ...

    return convertView;
}

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

@Override
public boolean isChildSelectable(int arg0, int arg1) {
    return false;
}

@Override
public void onGroupCollapsed(int groupPosition) {
    super.onGroupCollapsed(groupPosition);
}

@Override
public void onGroupExpanded(int groupPosition) {
    super.onGroupExpanded(groupPosition);
}
}

创建一个expandablelistview工作得很好,但是当我滚动列表时,我在getGroupView()中一直得到0的值。在我的logcat中,我希望看到groupPosition作为0, 1, 2, 3, 4..(当我向下滚动时),但它实际上显示0, 0, 0, 1, 0, 2, 0, 3, 0, 4..,在每个0之前都有额外的groupPosition。这只在第一次滚动时发生,所以如果我上下滚动几次,它不会显示额外的0值。
我用谷歌搜索了它,花了很多时间调试它,但我找不到任何解决方案。有人能帮我吗?
解决方案
我的问题在于Listview的布局。我把layout_height改成了match_parent,bug就消失了!我找到了解决方案。

最佳答案

我也有过类似的问题,得到了错误的观点。
不确定这里是否是这样,但我所做的是评论/删除行:

if (convertView == null) {

在ListAdapter中的getGroupView方法和getChildView方法中。
和OFC对应的末端花括号。
这与listadapter在显示视图时重用视图以减少系统资源的使用有关,但这使管理imo变得更困难。

10-08 12:07