6

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

有任何想法吗?

4

4 回答 4

3

确保你有这个布局

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

于 2016-04-01T13:30:29.720 回答
1

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

像那样:

listView = (ExpandableListView) findViewById(R.id.expandableListView);
listView.setOnGroupExpandListener(new OnGroupExpandListener() {
    @Override
    public void onGroupExpand(int groupPosition) {
        Log.d(TAG, "pos " + groupPosition);
    }
});
于 2012-05-24T02:17:51.170 回答
1

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

于 2015-03-12T05:41:41.563 回答
0
@Override
public Object getGroup(int groupPosition) {
    Log.i(TAG, "* getGroup : groupPosition =" + groupPosition);

    return categories[groupPosition];
}

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

在我的代码中,类别表示我作为组传递给可扩展列表的数据集。

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

@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
}

这是一个工作代码,希望你能理解它。

于 2012-05-24T03:09:44.553 回答