mikepenz / MaterialDrawer

The flexible, easy to use, all in one drawer library for your Android project. Now brand new with material 2 design.
https://mikepenz.dev
Apache License 2.0
11.67k stars 2.05k forks source link

Only one expanded item : how to ? #2806

Open engi opened 3 months ago

engi commented 3 months ago

About this issue

Hi Mike,

Question is in the title : how can I have only one expandable item expanded at one time? I have two expandable items (ids 2 and 3).

I tried this in onDrawerItemClickListener :

2L -> { //dictionnaries item
    val itemBackup: IDrawerItem<*>? = binding?.slider?.getDrawerItem(3L)
    if ((itemBackup != null) && itemBackup.isExpanded) {
        val positionItemBackup: Int = binding?.slider?.itemAdapter!!.getAdapterPosition(3)
        binding?.slider?.expandableExtension!!.collapse(positionItemBackup, true)
    }
}
3L -> { //backups item
    val itemDico: IDrawerItem<*>? = binding?.slider?.getDrawerItem(2L)
    if ((itemDico != null) && itemDico.isExpanded) {
        val positionItemDico: Int = binding?.slider?.itemAdapter!!.getAdapterPosition(2)
        binding?.slider?.expandableExtension!!.collapse(positionItemDico, true)
    }
}

or this :

binding?.slider?.apply {
    addItems(
        alarmDrawerItem,
        dicoDrawerItem,
        backupDrawerItem,
        settingsDrawerItem
    )
    expandableExtension.isOnlyOneExpandedItem = true
    setSavedInstance(savedInstanceState)
}

but it doesn't work !

Details

Checklist

mikepenz commented 2 months ago

You can retrieve the adapter from the MaterialDrawerSlidingView which exposes a expandableExtension API:

adapter.expandableExtension {
    isOnlyOneExpandedItem = true
}