Search code examples
javaandroidfragmentexpandablelistviewsubmenu

How can I make some of the Items in ExpandableListView Not Expandable


I am writing a navigation system using the ExpandableListView widget, and I am able to have expandable lists, that function properly, but what about items in the navigation that have no submenus like Home and just transition straight to the Home Fragment? All of the Group Items appear as dropdown-able even though they don't have any children. Can I take away the dropdown icon from the parents that don't have children? Or can I only either, have Icons or not have icons.


Solution

  • To disable the expanding of the group items, you could set a GroupClickListener and return true if you want to disable the expansion.

    ExpandableList.setOnGroupClickListener(new OnGroupClickListener() {
      @Override
      public boolean onGroupClick(ExpandableListView parent, View v,
                                  int groupPosition, long id) { 
         return groupPosition == 0; //Index of item in your list that you want to be expandable
      }
    });
    

    As for the icons, you could define your own layout for the group items.