Android Group Constant Position ExpandableListView 0

I have an ExpandableListView and I want to write a group clause when I click on a group. Unfortunately, the code below always returns 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 for groups and children that work correctly:

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?

+5
source share
4 answers

Make sure you have this for 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 cannot have an ExpandableListView in a ScrollView.

+3
source

your extended list view may be inside the scroll. You cannot have a nested scroll.

+1
source

listener OnGroupExpandListener, onGroupExpand - ExpandableListView.

Like:

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

    return categories[groupPosition];
}

When you continue BaseExpandableListAdapter, you will get the above override method. The "Logout" section above displays the number of the group pressed.

In my category code, means a dataset that I pass to an extensible list as a group.

You will have another override method call;

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

Using this method, you can call the following:

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

This is working code and I hope you can understand it.

0
source

All Articles