ExpandableLists in Android, I want to allow only one parent list to expand at a time

后端 未结 8 1685
难免孤独
难免孤独 2021-02-04 17:15

I want to make only one item in the parent list expand at a time, my code for the onCreate is currently the following. (It works as I want, But the method to allow only one pare

8条回答
  •  无人共我
    2021-02-04 17:53

    // Declare variable 
    private static int prev = -1;
    // and OnGroupExpandListener implement
    
    
     mExpandableList.setOnGroupExpandListener(new OnGroupExpandListener() {
    
                @Override
                public void onGroupExpand(int groupPosition) {
    
                    if(prev!=-1)
                    {
    
                        mExpandableList.collapseGroup(prev);    
    
                    }
                    prev=groupPosition;
                }
            });
    

    Updating with updated solution

    mExpandableList.setOnGroupExpandListener(new OnGroupExpandListener() {
        // Keep track of previous expanded parent
        int previousGroup = -1;
    
        @Override
        public void onGroupExpand(int groupPosition) {
            // Collapse previous parent if expanded.
            if ((previousGroup != -1) && (groupPosition != previousGroup)) {
                mExpandableList.collapseGroup(previousGroup);
            }
            previousGroup = groupPosition;
        }
    });
    

提交回复
热议问题