Android ExpandableListView 组位置始终为 0

标签 android expandablelistview

我有一个 ExpandableListView,我想在单击组时记录组位置。不幸的是,下面的代码总是返回 0,就好像我点击的是第 0 组。

  exList.setOnGroupClickListener(new OnGroupClickListener() {

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
          groupPosition = ExpandableListView.getPackedPositionGroup(id);

          Log.i("group position", groupPosition + "");
          return false;
    }

  });

我也有一个 longclicklistener,可以正常工作:

exList.setOnItemLongClickListener(new OnItemLongClickListener() {
      @Override
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
          if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
              groupPosition = ExpandableListView.getPackedPositionGroup(id);
...
}

有什么想法吗?

最佳答案

确保你有这个布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

您不能将 ExpandableListView 放入 ScrollView。

关于Android ExpandableListView 组位置始终为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10069892/

相关文章:

android - ExpandableListActivity 中 ContextMenu 的问题

java - 如何保存位图的正确旋转

Android 可扩展列表子布局未正确展开?

android - 更改可扩展 ListView 的图标

android - 如何在 2 个 ViewPagers 之间产生视差效果?

android - 单击图标 ExpandableListView

android - 如何防止 notifyDataSetChanged 折叠 ExpandableListView 中的打开组?

android - 在产品 flavor 中获取 gradle 构建类型

java - 如何在 Activity 开始时显示数字键盘?

android - Android Kotlin:如何从Firebase删除数据