如何在Kotlin中完全实现Navigation Drawer模板

时间:2019-06-11 19:12:26

标签: android xml kotlin

我希望这是一个非常简单的答案。我正在用android(一个运动追踪器)开发我的第一个真实应用程序,我希望它能为大多数应用程序提供一个导航抽屉布局。我有一堆页面,我想让抽屉导航到。我已经弄清楚了如何更改activity_main_drawer.xml菜单文件中菜单项的名称,但是当用户点击它们时,我不知道如何附加导航。我的代码是模板中的默认代码:

MainActivity.kt

package com.example.grahamfitnesstracker

import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.Snackbar
import android.support.v4.view.GravityCompat
import android.support.v7.app.ActionBarDrawerToggle
import android.view.MenuItem
import android.support.v4.widget.DrawerLayout
import android.support.design.widget.NavigationView
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.Menu

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)

        val fab: FloatingActionButton = findViewById(R.id.fab)
        fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show()
        }
        val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
        val navView: NavigationView = findViewById(R.id.nav_view)
        val toggle = ActionBarDrawerToggle(
            this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close
        )
        drawerLayout.addDrawerListener(toggle)
        toggle.syncState()

        navView.setNavigationItemSelectedListener(this)
    }

    override fun onBackPressed() {
        val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main, 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.
        return when (item.itemId) {
            R.id.action_settings -> true
            else -> super.onOptionsItemSelected(item)
        }
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        // Handle navigation view item clicks here.
        when (item.itemId) {
            R.id.nav_current_workout -> {
                // Handle the camera action
            }
            R.id.nav_log -> {

            }
            R.id.nav_exercises -> {

            }
            R.id.nav_workouts -> {

            }
            R.id.nav_stats -> {

            }
            R.id.nav_settings -> {

            }
        }
        val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
        drawerLayout.closeDrawer(GravityCompat.START)
        return true
    }
}

和导航抽屉菜单activity_main_drawer.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/nav_current_workout"
                android:icon="@drawable/ic_menu_play_filled"
                android:title="@string/menu_current_workout"/>
        <item
                android:id="@+id/nav_log"
                android:icon="@drawable/ic_menu_log"
                android:title="@string/menu_log"/>
        <item
                android:id="@+id/nav_exercises"
                android:icon="@drawable/ic_menu_weight"
                android:title="@string/menu_exercises"/>
        <item
                android:id="@+id/nav_workouts"
                android:icon="@drawable/ic_menu_person"
                android:title="@string/menu_workouts"/>
        <item
                android:id="@+id/nav_stats"
                android:icon="@drawable/ic_menu_chart"
                android:title="@string/menu_stats"/>
    </group>
    <item android:title="">
        <menu>
            <item
                    android:id="@+id/nav_settings"
                    android:icon="@drawable/ic_menu_settings"
                    android:title="@string/menu_settings"/>
        </menu>
    </item>

</menu

最后,我假定的content_main.xml是我需要放置片段的位置(我仍然不完全了解...)。从一个指南中,我将其更改为包括一个FrameLayout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/app_bar_main"
        tools:context=".MainActivity">

    <!-- Note : This is the container Frame Layout for all the fragments-->
    <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/mainFrame">
    </FrameLayout>


</android.support.constraint.ConstraintLayout>

我真的只是不知道要在MainActivity.kt的onNavigationItemSelected项中添加什么以便处理导航。我了解到我需要用放在页面用户界面中的某些片段替换content_main.xml中的内容,但是我不知道该怎么做。谁能帮我吗?我看了很多示例,但是它们通常实现自己的自定义导航栏,或者使用的Java使我感到困惑。

2 个答案:

答案 0 :(得分:0)

您是否已将big_array包含在NavigationView中,并将此activity_main添加到该视图中,如下所示:

nav_header_man

activity_main

并且由于您要求在MainActivity.kt中的onNavigationItemSelected项中添加什么内容,

<?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/app_bar_main" 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_main" app:menu="@menu/activity_main_drawer" > </com.google.android.material.navigation.NavigationView> </androidx.drawerlayout.widget.DrawerLayout>

MainActivity.kt

希望这会有所帮助。

答案 1 :(得分:0)

导航抽屉的Xml布局:

<include
    layout="@layout/dashboard_main"
    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"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/drawer_menu" />

在Kotlin文件中的实现:

 class MainActivity : 
 AppCompatActivity(),NavigationView.OnNavigationItemSelectedListener {
 private lateinit var drawer: DrawerLayout
 private lateinit var navigationView: NavigationView

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

    val toolbar: Toolbar = findViewById(R.id.toolbar_main)
    setSupportActionBar(toolbar)

    drawer = findViewById(R.id.drawer_layout)
    toggle = ActionBarDrawerToggle(
        dis,
        drawer,
        toolbar,
        R.string.navigation_drawer_open,
        R.string.navigation_drawer_close
    )
    drawer.addDrawerListener(toggle)
    toggle.setDrawerIndicatorEnabled(true)
    toggle.syncState()

    navigationView = findViewById(R.id.nav_view)
    navigationView.setNavigationItemSelectedListener(dis)

   }

   override fun onNavigationItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.login_signup -> {
        startActivity(Intent(dis, LoginActivity::class.java).putExtra("go_to","0"))
        }
    }
    drawer.closeDrawers()
    return true
   }
 }