android - ExpandableListView 中的 groupPosition 给了我错误的值

标签 android expandablelistview expandablelistadapter

编辑

我找到了解决方案,请看下面。

原创:

我创建了一个 ExpandableListView,它看起来像这样:

  • 第 0 组 - child 0、 child 1、 child 2
  • 第 1 组 - child 0、 child 1、 child 2、 child 3
  • 第 2 组 - child 0、 child 1、 child 2
  • 第 3 组 - child 0、 child 1、 child 2、 child 3、 child 4
  • ...

每个组 View 有3~5个 subview ,我强制展开所有组 View ,

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

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

解决方案

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

最佳答案

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

if (convertView == null) {

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

和 ofc 相应的结束大括号。

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

关于android - ExpandableListView 中的 groupPosition 给了我错误的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23399351/

相关文章:

java - 如何在 Android 中对组和子级可扩展 ListView 进行排序?

android - 折叠可扩展 ListView 后如何保留子选择

android - 以编程方式连接到蓝牙

android - "sendTextMessage"有些杀毒软件会判断为病毒,如何正确使用?

java - Android:获取 ExpandableListView 中的组数?

android - 可扩展 ListView ArrayIndexOutOfBoundsException

java - Expandablelistview subview 呈现两次(尽管高度设置为 match_parent)

java - 根据步行速度在 2 个 GPS 位置之间进行插值

java - 单击按钮时将项目添加到 RecyclerView

android - 将按钮添加到 expendableListView 作为最后一个子项