导航抽屉菜单项单击无响应

时间:2019-01-28 05:33:24

标签: android kotlin drawerlayout android-navigation-drawer

我正在开发一个android应用程序,并且是编程的新手。我已将基本抽屉活动扩展到所有活动。但是在扩展抽屉活动的活动中单击抽屉的菜单按钮后,什么也没发生。下面是我在基本抽屉活动中的代码。

open class DrawerActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    this.requestWindowFeature(Window.FEATURE_NO_TITLE)
    this.window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
    setContentView(R.layout.activity_drawer)
    setSupportActionBar(toolbar)
    nav_view.setNavigationItemSelectedListener(this)
    pageToGo = 1
    println("PageToGo on start activity: $pageToGo")
}

override fun onBackPressed() {
    println("Back is pressed")
    if (drawer_layout.isDrawerOpen(GravityCompat.END)) {
        drawer_layout.closeDrawer(GravityCompat.END)
    } else {
        finishAffinity()
    }
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    // Inflate the menu; this adds items to the action bar if it is present.
    menuInflater.inflate(R.menu.drawer, menu)
    return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    when (item.itemId) {
        R.id.action_settings -> return true
        else -> return super.onOptionsItemSelected(item)
    }
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    // Handle navigation view item clicks here.
    when (item.itemId) {
        R.id.nav_show_stat -> {
            println("I am clicked")
        }
        R.id.nav_restart_game -> {

        }
        R.id.nav_achievements -> {

        }
        R.id.nav_settings -> {

        }
        R.id.nav_about_us -> {

        }
        R.id.nav_contact_us -> {

        }
    }
    drawer_layout.closeDrawer(GravityCompat.END)
    return true
}

override fun setContentView(layoutResID:Int) {
    val fullLayout: DrawerLayout
    val actContent: FrameLayout
    fullLayout = layoutInflater.inflate(R.layout.activity_drawer, null) as DrawerLayout
    actContent = fullLayout.findViewById(R.id.act_content) as FrameLayout
    layoutInflater.inflate(layoutResID, actContent, true)
    super.setContentView(fullLayout)
}

下面是我的抽屉布局

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="end">

<include
    layout="@layout/app_bar_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<FrameLayout
    android:id="@+id/act_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_drawer"
    app:menu="@menu/activity_drawer_drawer">
</android.support.design.widget.NavigationView>

下面是我的菜单代码

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_show_stat"
        android:icon="@drawable/ic_menu_camera"
        android:title="@string/nav_show_stat" />
    <item
        android:id="@+id/nav_restart_game"
        android:icon="@drawable/ic_menu_gallery"
        android:title="@string/nav_restart" />
    <item
        android:id="@+id/nav_achievements"
        android:icon="@drawable/ic_menu_slideshow"
        android:title="@string/nav_achievements" />
    <item
        android:id="@+id/nav_settings"
        android:icon="@drawable/ic_menu_manage"
        android:title="@string/nav_settings" />
</group>

<item android:title="@string/nav_our_company">
    <menu>
        <item
            android:id="@+id/nav_about_us"
            android:icon="@drawable/ic_menu_share"
            android:title="@string/nav_about_us" />
        <item
            android:id="@+id/nav_contact_us"
            android:icon="@drawable/ic_menu_send"
            android:title="@string/nav_contact_us" />
    </menu>
</item>

下面是我在主要活动中的代码

class MainActivity : DrawerActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    load()
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    beginAdventure()
    main_layout.setOnTouchListener(object : OnSwipeTouchListener(applicationContext) {
        override fun onSwipeLeft() {
            drawer_layout.openDrawer(GravityCompat.END)
        }
    })
}

private fun beginAdventure() {
    start_game_btn.setSafeOnClickListener {
        nextPage("ChapterOneActOneActivity", 1)
    }
}
}

我知道我缺少重要的东西。但我只是不知道那是什么。 onNavigationItemSelected似乎不起作用,因为当我单击R.id.nav_show_stat时,永远不会打印“我被单击”。请让我知道如何使抽屉按钮在所有活动中都能正常工作。预先谢谢你。

Edit2 :我在主要活动中的抽屉 https://imgur.com/a/ofGzncZ

因此,当我单击“显示统计信息”时,将永远不会打印“我被点击”。如何处理点击事件以完成我想要的事情?

1 个答案:

答案 0 :(得分:0)

不需要 分别使用以下给定的代码:-

override fun setContentView(layoutResID:Int) {
    val fullLayout: DrawerLayout
    val actContent: FrameLayout
    fullLayout = layoutInflater.inflate(R.layout.activity_drawer, null) as DrawerLayout
    actContent = fullLayout.findViewById(R.id.act_content) as FrameLayout
    layoutInflater.inflate(layoutResID, actContent, true)
    super.setContentView(fullLayout)
}

只需添加 :-

setContentView(R.layout.your_layout_filename);

我认为您的代码中没有其他问题