Ich habe in Kotlin ein Schubladenmenü erstellt und möchte diese Menüpunkte verwenden. In Java habe ich die onNavigationItemSelected
Methode aufgerufen, aber wenn ich sie in Kotlin verwenden möchte, wird sie nicht angezeigt. Hier ist mein Code:
<android.support.v4.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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerLayout"
tools:context="com.example.zamknijryjx.liobrus.UserActivity">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/imie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="40dp"
android:text="Imie"
android:textSize="50sp"
android:textColor="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu"
android:layout_gravity="start"
app:headerLayout="@layout/navigation_header">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Hier ist navigation_menu.xml
:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/nav_home"
android:title="Home"/>
<item android:id="@+id/nav_sprawdziany"
android:title="Sprawdziany"/>
<item android:id="@+id/nav_prace"
android:title="Prace klasowe"/>
</menu>
Und Code in meiner Aktivität:
mToggle = ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close)
drawerLayout.addDrawerListener(mToggle!!)
mToggle!!.syncState()
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
if (mToggle!!.onOptionsItemSelected(item)) {
return true
}
return super.onOptionsItemSelected(item)
}
Also möchte ich so etwas machen: Wenn der Benutzer zum Beispiel auf den Menüpunkt mit der ID klickt nav_home
, wird Toast gemacht. Vielen Dank für die Hilfe!
Sie können Ihre NavigationView
ID in Ihrer Layoutdatei angeben:
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
... >
</android.support.design.widget.NavigationView>
Und fügen Sie dann einen Listener hinzu in Ihrem UserActivity
:
navigationView.setNavigationItemSelectedListener {
when (it.itemId) {
R.id.nav_home -> {
// handle click
true
}
else -> false
}
}
Dieser Artikel stammt aus dem Internet. Bitte geben Sie beim Nachdruck die Quelle an.
Bei Verstößen wenden Sie sich bitte [email protected] Löschen.
Lass mich ein paar Worte sagen