8

我正在使用 Androidx,带有导航抽屉的 android jetpack 导航不显示 mobile_navigation.xml 中给出的后退按钮和标题,我尝试使用 nav_view.setupWithNavController(navController) 的导航不起作用,工作室显示未解析的符号。我已经为项目添加了所需的依赖项

Gradle.build :

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.qbitstream.salesmanagementsystem"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    def nav_version = "1.0.0-alpha04"
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-rc01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-rc01'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0-rc01'
    implementation 'com.google.android.material:material:1.0.0-alpha1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
    implementation "android.arch.navigation:navigation-fragment:$nav_version" // use -ktx for Kotlin
    implementation "android.arch.navigation:navigation-ui:$nav_version" 
    def room_version = "2.0.0-rc01"
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-rxjava2:$room_version"
    implementation "androidx.room:room-guava:$room_version"
    testImplementation "androidx.room:room-testing:$room_version"
    implementation "com.squareup.retrofit2:retrofit:2.3.0"
    implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0"
    implementation "com.squareup.retrofit2:converter-gson:2.3.0"
    implementation "io.reactivex.rxjava2:rxandroid:2.0.1"
    implementation 'org.jetbrains.anko:anko-common:0.9'

}

这是我的 HomeActivity:

import android.os.Bundle
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.NavigationUI
import kotlinx.android.synthetic.main.activity_home.*
import kotlinx.android.synthetic.main.app_bar_home.*

class HomeActivity : AppCompatActivity() {
private lateinit var  navController:NavController

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_home)
    setSupportActionBar(toolbar)
    val toggle = ActionBarDrawerToggle(this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
    drawer_layout.addDrawerListener(toggle)
    toggle.syncState()
    val host = supportFragmentManager.findFragmentById(R.id.m_fragment) as NavHostFragment? ?: return
    navController = host.navController
    NavigationUI.setupWithNavController(nav_view,navController)

}
override fun onBackPressed() {
    if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
        drawer_layout.closeDrawer(GravityCompat.START)
    } else {
        super.onBackPressed()
    }
}
override fun onSupportNavigateUp(): Boolean {
    return NavigationUI.navigateUp(drawer_layout, navController) || super.onSupportNavigateUp()
}

抽屉菜单.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/homeFragment"
            android:icon="@drawable/order"
            android:title="Home" />
        <item
            android:id="@+id/salesOrderFragment"
            android:icon="@drawable/order"
            android:title="Order" />
        <item
            android:id="@+id/nav_gallery"
            android:icon="@drawable/bill"
            android:title="Bill Collection" />
        <item
            android:id="@+id/nav_slideshow"
            android:icon="@drawable/bill"
            android:title="Sales History" />
        <item
            android:id="@+id/logoutFragment"
            android:icon="@drawable/order"
            android:title="Logout" />
    </group>
</menu>

这是 mobile_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/main_nav"
    app:startDestination="@+id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.qbitstream.salesmanagementsystem.ui.home.HomeFragment"
        android:label="Home"
        tools:layout="@layout/fragment_home"/>
    <fragment
        android:id="@+id/salesOrderFragment"
        android:name="com.qbitstream.salesmanagementsystem.ui.order.SalesOrderFragment"
        android:label="Order"
        tools:layout="@layout/fragment_sales_order" />
    <fragment
        android:id="@+id/logoutFragment"
        android:name="com.qbitstream.salesmanagementsystem.LogoutFragment"
        android:label="fragment_logout"
        tools:layout="@layout/fragment_logout" />
</navigation>
4

2 回答 2

6

需要同时使用这两个功能

NavigationUI.setupWithNavController (toolbar, navController, drawer_layout)
NavigationUI.setupWithNavController(nav_view,navController)
于 2018-08-17T09:00:07.750 回答
4

你需要使用NavigationUI.setupActionBarWithNavController()

  • 设置返回的 ActionBar 以AppCompatActivity.getSupportActionBar()NavController.

  • 通过调用这个方法,当目的地改变时,动作栏中的标题会自动更新(假设有一个有效的标签)。

  • 当您在非根目的地时,actionBar也会显示 Up 。button调用navigateUp(DrawerLayout, NavController)处理向上按钮

示例代码

对于科特林

NavigationUI.setupActionBarWithNavController(this, navHost.navController)

对于 Java

NavigationUI.setupActionBarWithNavController(this, navController.getNavController());

编辑

不要忘记覆盖 onSupportNavigateUp()方法

fun onSupportNavigateUp(): Boolean {
   // return navHost.navController.navigateUp() || super.onSupportNavigateUp()
    return NavigationUI.navigateUp(drawer, navHost.navController) || super.onSupportNavigateUp()
}

编辑 2

与导航抽屉一起使用您需要使用NavigationUI.setupWithNavController()

  • 通过调用此方法,当目标更改时,工具栏中的标题将自动更新(假设存在有效标签)。

对于科特林

NavigationUI.setupWithNavController(toolbar, navController, drawer_layout)

对于 Java

NavigationUI.setupWithNavController(toolbar, navController.getNavController(),drawer_layout);
于 2018-08-17T07:09:43.503 回答