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

onDrawerItemClickListener Not invoked when item clicked #2737

Closed Chullian closed 3 years ago

Chullian commented 3 years ago

About this issue

binding.homeDrawer.apply {
            itemAdapter.add(
                PrimaryDrawerItem().apply {
                    nameText = "HOME"; identifier = 1
                },
                PrimaryDrawerItem().apply {
                    nameText = "LOGOUT"; identifier = 2
                })
             onDrawerItemClickListener = { _,drawerItem, _ ->
                when  {
                    drawerItem.identifier == 1L -> {
                        Toast.makeText(this@HomeActivity, "clicked home", Toast.LENGTH_SHORT).show()
                    }
                    drawerItem.identifier == 2L -> {
                        Toast.makeText(this@HomeActivity, "clicked logout", Toast.LENGTH_SHORT).show()
                    }
               }}
  }

this is my drawer creation and click listener, but the click event is not getting triggered. is there anything wrong here?

mikepenz commented 3 years ago

@Chullian tried your code, which won't compile as the when is not exhaustive and the boolean return value is missing.

binding.slider.apply {
            itemAdapter.add(
                PrimaryDrawerItem().apply {
                    nameText = "HOME"; identifier = 1
                },
                PrimaryDrawerItem().apply {
                    nameText = "LOGOUT"; identifier = 2
                })
            onDrawerItemClickListener = { _, drawerItem, _ ->
                when {
                    drawerItem.identifier == 1L -> {
                        Toast.makeText(this@DrawerActivity, "clicked home", Toast.LENGTH_SHORT).show()
                    }
                    drawerItem.identifier == 2L -> {
                        Toast.makeText(this@DrawerActivity, "clicked logout", Toast.LENGTH_SHORT).show()
                    }
                    else -> {}
                }
                false
            }
        }

after fixing this, the listener is correctly triggered though in the sample.

I'd assume either the return of the listener is off, or maybe the DrawerLayout itself is not clickable or such.

Chullian commented 3 years ago

@mikepenz

maybe the DrawerLayout itself is not clickable or such.

means? is there any other step I am missing here? because when i added else and return statement it still same

mikepenz commented 3 years ago

Just mentioning guesses, as noted above. using the sample code, everything works perfectly fine if used in the sample.

If you have a full compilable runnable sample to showcase the issue, I can have a closer look.

Chullian commented 3 years ago

closing this issue since i have found the problem. It was , a viewGroup below the drawerSlider view. Thank you Mike for this amazing library