簡體   English   中英

帶導航抽屜的Jetpack導航不顯示androidx中子片段上的mobile_navigation.xml中給出的后退按鈕和標題

[英]Jetpack navigation with navigation drawer not display back button and title given in the mobile_navigation.xml on child fragment in androidx

我正在使用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()
}

抽屜_menu.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>

需要使用這兩個功能

NavigationUI.setupWithNavController (toolbar, navController, drawer_layout)
NavigationUI.setupWithNavController(nav_view,navController)

你需要使用NavigationUI.setupActionBarWithNavController()

  • 設置AppCompatActivity.getSupportActionBar()返回的ActionBar,以便與NavController一起使用。

  • 通過調用此方法,當目標更改時(假設存在有效標簽),操作欄中的標題將自動更新。

  • 當您在非根目標上時, actionBar還將顯示“向上” button 調用navigateUp(DrawerLayout, NavController)來處理Up按鈕

示例代碼

對於Kotlin

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()

  • 通過調用此方法,當目標更改時(假設存在有效標簽),工具欄中的標題將自動更新。

對於Kotlin

NavigationUI.setupWithNavController(toolbar, navController, drawer_layout)

對於Java

NavigationUI.setupWithNavController(toolbar, navController.getNavController(),drawer_layout);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM