1

Drawer Navigation works normally, but when an item is clicked, no event is generated and Navigation is terminated. Which event listener should I use? I want to use the Drawer Navigation by clicking on the imageview. The opening of Drawer Navigation was successful, but when the menu is clicked after opening, a click event is not generated and the navigation is terminated. How do we solve this problem?

layout_test.xml

<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".Activities.test">
<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:fitsSystemWindows="true"
    app:menu="@menu/test_menu"
    app:headerLayout="@layout/header_test"/>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<ImageView
    android:id="@+id/menu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="20dp"
    android:src="@drawable/ico_menu_b"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent">
</ImageView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>

header_test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/black"
android:gravity="center"
android:orientation="vertical"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Header"
    android:textSize="20sp"
    android:textStyle="bold"/>
</LinearLayout>

test_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu1"
    android:title="work1"
    android:enabled="true"
    android:icon="@drawable/notice_logo"/>
<item android:id="@+id/menu2"
    android:title="work2"
    android:enabled="true"
    android:icon="@drawable/my_page"/>
<item android:id="@+id/logout_menu"
    android:title="logout"
    android:enabled="true"
    android:icon="@drawable/ico_out"/>
</menu>

test.kt

 class test : AppCompatActivity() , NavigationView.OnNavigationItemSelectedListener{

    private lateinit var binding:  LayoutTestBinding

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.layout_test)

    binding = LayoutTestBinding.inflate(layoutInflater)
    setContentView(binding.root)



    binding.menu.setOnClickListener {
        binding.drawerLayout.openDrawer(GravityCompat.END)
    }

 }

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    val inflater = menuInflater
    inflater.inflate(R.menu.detail_appbar_menu, menu)
    val logouticon = menu?.findItem(R.id.logout_menu)
    logouticon?.actionView?.setOnClickListener {
        onOptionsItemSelected(logouticon)
    }
    val menu2 = menu?.findItem(R.id.menu2)
    menu2?.actionView?.setOnClickListener {
        onOptionsItemSelected(menu2)
    }
    val menu1 = menu?.findItem(R.id.menu1)
    menu1?.actionView?.setOnClickListener {
        onOptionsItemSelected(menu1)
    }
    return super.onCreateOptionsMenu(menu)
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    when(item.itemId){
        R.id.logout_menu -> {
            Toast.makeText(this@test,"logout", Toast.LENGTH_SHORT).show()
        }
        R.id.menu2 -> {
            Toast.makeText(this@test,"dddddd", Toast.LENGTH_SHORT).show()
        }
        R.id.menu1 -> {
            Toast.makeText(this@test,"?????", Toast.LENGTH_SHORT).show()
        }
    }
    return false
    }
 }
jyk
  • 21
  • 4

1 Answers1

0

you have to use this line for the same in onCreate method

binding.navview.setNavigationItemSelectedListener(this)

so your onCreate function will be look like this

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.layout_test)

    binding = LayoutTestBinding.inflate(layoutInflater)
    setContentView(binding.root)
    binding.navview.setNavigationItemSelectedListener(this)

    binding.menu.setOnClickListener {
        binding.drawerLayout.openDrawer(GravityCompat.END)
    }

}

and it will work.

Mr. Mad
  • 1,230
  • 1
  • 14
  • 28
  • Yes, I'm not talking about popping up the drawer layout, but triggering an event when a menu item is clicked after it's popped up! – jyk Sep 29 '22 at 00:57