【问题标题】:How to fully implement the Navigation Drawer template in Kotlin如何在 Kotlin 中完全实现 Navigation Drawer 模板
【发布时间】:2019-06-11 19:12:26
【问题描述】:

我希望这是一个非常简单的答案。我正在开发我的第一个真正的 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 中的内容替换为我放入页面 ui 的一些片段,但我不知道该怎么做。谁能帮我吗?我看过一堆例子,但他们通常实现自己的自定义导航栏,或者使用 java,这让我作为一个新手感到困惑。

【问题讨论】:

  • open android studio -> File -> new-> new Project -> Selcet Navigation Drawer Activity 模板然后点击下一步 -> 选择语言 kotlin 然后点击 finist -> 你会得到示例代码具有最新架构组件的导航抽屉:)

标签: android xml kotlin


【解决方案1】:

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

activity_main

<?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 中的 onNavigationItemSelected 项目中添加什么,

MainActivity.kt

override fun onNavigationItemSelected(item: MenuItem): Boolean {
        // Handle navigation view item clicks here.
        val i = Intent()
        when (item.itemId) {
            R.id.nav_current_workout -> {
                i.setClass(this, CurrentWorkoutActivity::class.java)
                startActivity(i)
            }
            R.id.nav_log -> {
                //similarly start activity with Intent 
            }
            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
    }
}

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    导航抽屉的 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
       }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-17
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多