Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ExpandableListView group position always 0

I have an ExpandableListView and I want to log the groupposition when clicking on a group. Unfortunately the code below returns always 0, as if I were clicking on the 0th group.

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

  });

I also have a longclicklistener on the groups and childs which works right:

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);
...
}

Any ideas?

like image 555
erdomester Avatar asked Apr 09 '12 06:04

erdomester


3 Answers

Make sure you have this for the layout

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

You can't have a ExpandableListView into a ScrollView.

like image 109
Sigvent Avatar answered Nov 08 '22 11:11

Sigvent


Use the listener OnGroupExpandListener, the param of the method onGroupExpand is the position of the group at the ExpandableListView.

Like that:

listView = (ExpandableListView) findViewById(R.id.expandableListView);
listView.setOnGroupExpandListener(new OnGroupExpandListener() {
    @Override
    public void onGroupExpand(int groupPosition) {
        Log.d(TAG, "pos " + groupPosition);
    }
});
like image 43
Derzu Avatar answered Nov 08 '22 11:11

Derzu


your expandable list view might be inside a scroll view. You can't have nested scroll.

like image 1
navalkishoreb Avatar answered Nov 08 '22 13:11

navalkishoreb