导航抽屉中的onClick事件不起作用

时间:2019-01-04 14:48:43

标签: java android kotlin navigation-drawer android-navigation-drawer

免责声明:我已经阅读了onClick event in navigation drawer上的帖子,但遗憾的是它对我不起作用。

我对Android还是很陌生,我刚刚在Kotlin制作了我的第一个应用程序,我想集成一个NavigationDrawer。在其中,我集成了一个“网站”选项,应该可以启动一个网站。只是当我单击它时,它关闭了抽屉,但没有启动网站的意图。

我的代码:(我删除了应用程序其余部分所需的不重要部分)

MainActivity.kt:

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

private lateinit var drawerLayout: DrawerLayout
private lateinit var aToggle: ActionBarDrawerToggle

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(activity_main)
    createNotificationChannel()
    setNavigationViewListener()

    val toolbar: Toolbar = toolbar
    setSupportActionBar(toolbar)
    val actionbar: ActionBar? = supportActionBar
    actionbar?.apply {
        setDisplayHomeAsUpEnabled(true)
        setHomeAsUpIndicator(R.drawable.ic_menu)
    }

    drawerLayout = drawer_layout
    aToggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close)
    drawerLayout.addDrawerListener(aToggle)

    drawerLayout.addDrawerListener(
            object : DrawerLayout.DrawerListener {
                override fun onDrawerSlide(drawerView: View, slideOffset: Float) {

                }

                override fun onDrawerOpened(drawerView: View) {

                }

                override fun onDrawerClosed(drawerView: View) {

                }

                override fun onDrawerStateChanged(newState: Int) {

                }
            }
    )

    aToggle.syncState()
    val navigationView: NavigationView = findViewById(R.id.nav_view)
    navigationView.setNavigationItemSelectedListener { menuItem ->
        // set item as selected to persist highlight
        menuItem.isChecked = true
        // close drawer when item is tapped
        drawerLayout.closeDrawers()

        // Add code here to update the UI based on the item selected
        // For example, swap UI fragments here

        true
    }
}

private fun setNavigationViewListener() {
    val navigationView = nav_view
    navigationView.setNavigationItemSelectedListener(this)
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    val inflater = menuInflater
    inflater.inflate(R.menu.drawer_view, menu)
    return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    return when (item.itemId) {
        android.R.id.home -> {
            drawerLayout.openDrawer(GravityCompat.START)
            true
        }
        else -> super.onOptionsItemSelected(item)
    }
    if(aToggle.onOptionsItemSelected(item)) {
        return true
    }
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.website -> {
            val i = Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://gym-wen.de/vp/"))
            startActivity(i)
            drawerLayout.closeDrawer(GravityCompat.START)
            return true
        }
        else -> {}
    }
    return true
}
}

activity_main.xml:

<?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:layout_width="match_parent"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

    <ListView
        android:id="@+id/vertretungs_list"
        android:paddingTop="?attr/actionBarSize"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</FrameLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/drawer_view" />

drawer_view.xml(菜单):

<?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/website"
        android:title="@string/website" />
    <item
        android:id="@+id/nav_gallery"
        android:title="2" />
    <item
        android:id="@+id/nav_slideshow"
        android:title="3" />
    <item
        android:id="@+id/nav_manage"
        android:title="4" />
</group>

我很确定我忘记了一些东西,但是我只是不知道那是什么。我读了许多教程和其他文章,但我只是不知道。希望有人可以帮助我:)

问候

2 个答案:

答案 0 :(得分:0)

您为相同的内容设置了两个侦听器。一个覆盖另一个。

navigationView.setNavigationItemSelectedListener { menuItem ->
    // set item as selected to persist highlight
    menuItem.isChecked = true
    // close drawer when item is tapped
    drawerLayout.closeDrawers()

    // Add code here to update the UI based on the item selected
    // For example, swap UI fragments here

    true
}

哪个不启动网站,navigationView.setNavigationItemSelectedListener(this)不应该启动网站。

我是您,我将删除整个第一块并在override fun onOptionsItemSelected(item: MenuItem): Boolean中实现所有内容

答案 1 :(得分:0)

您的活动类实现NavigationView.OnNavigationItemSelectedListener
因此,而不是设置为侦听器:

navigationView.setNavigationItemSelectedListener { menuItem ->
    // set item as selected to persist highlight
    menuItem.isChecked = true
    // close drawer when item is tapped
    drawerLayout.closeDrawers()

    // Add code here to update the UI based on the item selected
    // For example, swap UI fragments here

    true
}

您应该致电:

navigationView.setNavigationItemSelectedListener(this)

因此该方法称为:

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.website -> {
            val i = Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://gym-wen.de/vp/"))
            startActivity(i)
            drawerLayout.closeDrawer(GravityCompat.START)
            return true
        }
        else -> {}
    }
    return true
}

,然后通过这种方法来控制抽屉或已选中项目的关闭。