Expand one item only inside expandable item without being collapsed

1.4k views Asked by At

I have a drawer with multi expandable item inside each other, for an example "main1" (expandable item) inside it "main2-1","main2-2","main2-3" (expandable item) inside them "main3-1","main3-2","main3-3" (normal item)

I want to let only one ExpandedItem at the level of "main2" (main2-1, 2-1, 2-3) also at the level of "main1"

I have tried result.getAdapter().withOnlyOneExpandedItem(true); where "result" is the drawer library I am using - It doesn't work cuz collapsing "main2" level instead of expanding the first item of the level

Also, this method doesn't work in the 3rd click of different expandable items - I followed th code up with Logging to be sure the app enters it but it doesn't collapse the previous one

if(previousExpandableItemPosition != position) {
   result.getAdapter().collapse(previousExpandableItemPosition);
   previousExpandableItemPosition = position;
}
1

There are 1 answers

0
Dasser Basyouni On BEST ANSWER

I have made a method which i placed in the onClick of the expanding items i want to have only one of them expanded and here it is to help anybody other

if (position != previousPosition && previousPosition != 0 && previousIdentifier != drawerItem.getIdentifier()) { // WithOnlyOneExpandableItem
                                IDrawerItem itemx = result.getAdapter().getItem(previousPosition);
                                if (itemx.isExpanded()) {
                                    itemx.withIsExpanded(true);
                                    result.updateItem(itemx);
                                } else {
                                    if(previousPosition > position){ // user is going upward
                                        itemx = result.getAdapter().getItem(previousPosition + result.getAdapter().getItem(position).getSubItems().size());
                                        if (itemx.isExpanded()) {
                                            itemx.withIsExpanded(true);
                                            result.updateItem(itemx);
                                        }else{Log.i("ppppppppppppp", String.valueOf(previousPosition + (previousPosition - position)));}

                                    }else if(previousPosition > position){ // user is going downward
                                        itemx = result.getAdapter().getItem(previousPosition + (previousPosition
                                                + result.getAdapter().getItem(previousPosition).getSubItems().size()));
                                        if (itemx.isExpanded()) {
                                            itemx.withIsExpanded(true);
                                            result.updateItem(itemx);
                                        }else{Log.i("ppppppppppppp", String.valueOf(previousPosition + (previousPosition - position)));}
                                    }
                                }
                            } else if (previousPosition != 0 && previousIdentifier == drawerItem.getIdentifier()) {
                                //if the drawer item is collapsed after expanding then deselect it
                                EnterTimes++;
                                if(!result.getAdapter().getItem(position).isExpanded()){
                                    EnterTimes=1;
                                }
                                if(EnterTimes <2){
                                    result.getAdapter().getItem(position).withSetSelected(false);
                                    result.updateItem(drawerItem);
                                }
                            }
                            previousIdentifier = (int) drawerItem.getIdentifier();
                                previousPosition = position;

And before onCreate int previousPosition = 0, previousIdentifier = 0;