【问题标题】:groupPosition in ExpandableListView gives me wrong valueExpandableListView 中的 groupPosition 给了我错误的值
【发布时间】:2014-04-30 22:45:16
【问题描述】:

编辑

我找到了解决办法,请看下文。

原文:

我创建了一个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..,在每个 groupPosition 之前都有额外的 0。这只发生在第一次滚动时,所以如果我上下滚动几次,它不会显示额外的 0 值。

我用谷歌搜索并花了很多时间调试它,但我找不到任何解决方案。谁能帮帮我?

解决方案

我的问题出在Listview 的布局中。我把layout_height改成match_parent,bug就消失了!我找到了解决方案here。

【问题讨论】:

    标签: android expandablelistview expandablelistadapter


    【解决方案1】:

    我曾经遇到过类似的问题,得到了错误的观点。 不确定这里是否是这种情况,但我所做的是评论/删除该行:

    if (convertView == null) {
    

    在 getGroupView 方法和 listadapter 中的 getChildView 方法中。

    和ofc对应的结束大括号。

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

    【讨论】:

    • 感谢您的回答,但这不是我的解决方案。我已经编辑了我的原始问题并添加了我的修复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    • 2013-05-25
    • 2014-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多