zsmb13 / MaterialDrawerKt

A Kotlin DSL wrapper around the mikepenz/MaterialDrawer library.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Switch item onClick listeners not called

johnwilde opened this issue · comments

I'd like to launch a fragment when the text of a switch item is clicked (and toggle the switch when the switch itself is clicked). However, it seems that when the item is bound the click listener set here is overridden.

switchItem {"Switch 1"
    selectable = true
    onClick { view ->
        Log.d("Drawer", "Clicked!")
        false
    }
}
switchItem {"Switch 2"
    selectable = false
    onClick { item ->
        Log.d("Drawer", "Clicked!")
        false
    }
}

The onClick function is not called in either of these cases. It seems there was an old issue that suggests it is possible, but I'm not sure how to do it. Any ideas?
mikepenz/MaterialDrawer#321

                onBindView { drawerItem, _ ->
                    val switchDrawerItem = (drawerItem as SwitchDrawerItem)
                    switchDrawerItem.withOnDrawerItemClickListener { _, _, _ ->
                        Log.d("Drawer", "Clicked!")
                        false
                    }
                }

This gives me the desired behavior, but wondering if there's a better way.

Indeed, I found the same thing, no matter what you do, it gets overriden here. The onBindView solution is a pretty smart one, I don't think there's anything better available currently.

Perhaps it's worth raising an issue (or PR) with the original library to wrap the listener inside the bindView method instead of replacing it.