ExpandableListView中的groupPosition给了我错误的值

时间:2014-04-30 22:45:17

标签: android expandablelistview expandablelistadapter

修改

我找到了解决方案,请参阅下文。

ORIGINAL:

我创建了一个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中,我希望看到groupPosition0, 1, 2, 3, 4..(当我向下滚动时),但实际上显示0, 0, 0, 1, 0, 2, 0, 3, 0, 4..在每个0之前有groupPosition }。这只发生在第一次滚动时,所以如果我多次向上和向下滚动,它就不会显示额外的0值。

我用Google搜索并花了很多时间来调试它,但我找不到任何解决方案。任何人都可以帮助我吗?

解决方案

我的问题出现在Listview的布局中。我将layout_height更改为match_parent,错误消失了!我找到了解决方案here

1 个答案:

答案 0 :(得分:4)

我有一次类似的问题,得到了错误的观点。 不确定这是否是这种情况,但我做的是评论/删除该行:

if (convertView == null) {
在listadapter中的getGroupView方法和getChildView方法中都

和ofc相应的结束大括号。

它与listadapter在显示视图时重用视图以使用较少的系统资源有关,但这使得管理imo变得更加困难。