【问题标题】:Android ExpandableListView group position always 0Android ExpandableListView 组位置始终为 0
【发布时间】:2012-04-09 06:47:26
【问题描述】:

我有一个 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);
...
}

有什么想法吗?

【问题讨论】:

  • 您是否为 ExpandableListView 使用自定义适配器?
  • 是的。我的其他听众工作正常
  • 好的,确保您的 getItem 和 getItemId 方法返回有效值而不是 0 ..(在您的自定义适配器中)您还可以查看此适配器 developer.android.com/resources/samples/ApiDemos/src/com/…
  • public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) 确实返回了组位置。
  • 我不知道,但如果我不定义 groupPosition,它工作正常...

标签: android expandablelistview


【解决方案1】:

确保你有这个布局

<?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。

【讨论】:

    【解决方案2】:

    使用监听器OnGroupExpandListener,onGroupExpand方法的参数是组在ExpandableListView的位置。

    这样:

    listView = (ExpandableListView) findViewById(R.id.expandableListView);
    listView.setOnGroupExpandListener(new OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            Log.d(TAG, "pos " + groupPosition);
        }
    });
    

    【讨论】:

      【解决方案3】:

      您的可展开列表视图可能位于滚动视图中。你不能有嵌套滚动。

      【讨论】:

      • 谢谢!我需要这个。我只是不明白我做错了什么。我就是这种情况。
      • 很高兴为您提供帮助...!! :)
      【解决方案4】:
      @Override
      public Object getGroup(int groupPosition) {
          Log.i(TAG, "* getGroup : groupPosition =" + groupPosition);
      
          return categories[groupPosition];
      }
      

      当你扩展 BaseExpandableListAdapter 时,你会得到上面的覆盖方法。上面的 Log out put 清楚地显示了点击的组号。

      在我的代码中,categories 表示我作为组传递给 Expandable 列表的数据集。

      您将有另一个覆盖方法调用;

      @Override
      public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
      ...
      }
      

      使用该方法,您可以如下调用;

      if(getGroup(groupPosition).toString().equals("GroupName")){
          //Do what even you like in for the clicked group
      }
      

      这是一个有效的代码,希望你能理解。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-19
        相关资源
        最近更新 更多