尽管导航抽屉可见并打开,但侧面导航抽屉中的项目单击不起作用

问题描述 投票:0回答:1

我正在尝试实现导航抽屉,但是当我运行我的应用程序并单击抽屉中的项目时,它只是关闭了抽屉而不显示烤面包。单击项目进行导航时,抽屉没有任何作用,即导航抽屉中的内容无法单击,即菜单。

下面是我的导航抽屉代码:

Activity Main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:openDrawer="start">

    <include
        layout="@layout/content_menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/nav_menu"/>
<LinearLayout 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"
    tools:context=".FirstMain"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:background="@color/back"
        android:fontFamily="@font/berkshire_swash"
        android:gravity="center"
        android:text="@string/app_name"
        android:textAlignment="center"
        android:textColor="@color/txt"
        android:textSize="50sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button"
        android:layout_width="165dp"
        android:layout_height="wrap_content"
        android:layout_margin="30sp"
        android:background="@color/back"
        android:fontFamily="@font/berkshire_swash"
        android:gravity="center_horizontal"
        android:padding="10sp"
        android:text="PLAY"
        android:textColor="@color/txt"
        android:textSize="35sp"
        android:textStyle="bold" />

</LinearLayout>

</androidx.drawerlayout.widget.DrawerLayout>

nav_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">

    <item
        android:id="@+id/nav_rule"
        android:icon="@drawable/rules"
        android:title="Rules" />
    <item
        android:id="@+id/nav_about"
        android:icon="@drawable/about"
        android:title="About"/>
</group>
</menu>

Content_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.AppBarLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/puzzle">

    </LinearLayout>

</LinearLayout>

Main.kt

class FirstMain : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener{
    lateinit var btn : Button
    lateinit var toolbar: Toolbar
    lateinit var drawerLayout: DrawerLayout
    lateinit var navView: NavigationView

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

        btn = findViewById(R.id.button)
        toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)

        drawerLayout = findViewById(R.id.drawer_layout)
        navView = findViewById(R.id.nav_view)

        val toggle = ActionBarDrawerToggle(
            this, drawerLayout, toolbar, 0, 0
        )
        drawerLayout.addDrawerListener(toggle)
        toggle.syncState()

        btn.setOnClickListener {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }
        navView.setNavigationItemSelectedListener(this)
    }

    override fun onNavigationItemSelected(p0: MenuItem): Boolean {
        p0.isChecked = true
        when (p0.itemId) {
            R.id.nav_rule -> {
                Toast.makeText(this, "Profile clicked", Toast.LENGTH_LONG).show()
            }
            R.id.nav_about -> {
                Toast.makeText(this, "Messages clicked", Toast.LENGTH_LONG).show()
            }
        }
        drawerLayout.closeDrawer(GravityCompat.START)
        return true
    }

}
android android-studio kotlin navigation-drawer
1个回答
0
投票

您的代码看起来很好,很奇怪,它不起作用,但是您可以尝试添加

navView.bringToFront();
navView.setNavigationItemSelectedListener(this)
© www.soinside.com 2019 - 2024. All rights reserved.