How to Setup Jetpack Navigation with material.Bott

2020-08-16 07:51发布

问题:

I am unable to setup Navigation component with com.google.android.material.bottomnavigation.BottomNavigationView it is giving an error.

Here is the code that I am trying to use

 // Setup bottom navigation view
    NavigationUI.setupWithNavController(
            bottom_navigation_view,
            findNavController(R.id.main_nav_host_fragment)
    )

when I open the NavigationUI class, I note that it just accept the old android.support.design.widget.BottomNavigationView but I want to use the new one com.google.android.material.bottomnavigation.BottomNavigationView class. Any idea about it?

[Update 14/11/2019]

This was a bug and was fixed at Android Gradle Plugin (~3.2.0-rc-01 and 3.3.0-alpha-04). You can see the issue tracker in the following link: https://issuetracker.google.com/issues/110692942

回答1:

I am using these libraries

implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
// Architecture Components
def nav_version = "1.0.0-alpha02"
implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
// use -ktx for Kotlin
implementation "android.arch.navigation:navigation-ui-ktx:$nav_version"

and this is my Activity

NavigationUI.setupWithNavController(bottomNavView, 
Navigation.findNavController(this, R.id.nav_host_fragment))

and this is my layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout  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:layout_height="match_parent"
tools:context=".ui.main.ActivityMain">

<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottomNavView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:menu="@menu/menu_nav_drawer" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
    style="@style/Widget.MaterialComponents.FloatingActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_marginEnd="@dimen/dimen_16"
    android:layout_marginBottom="72dp"
    app:fabSize="normal"
    tools:srcCompat="@drawable/ic_add_white" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>


回答2:

Please update "android.arch.navigation:navigation-ui:1.0.0-alpha03" for solve this problem

NavigationUI updated



回答3:

Looking at the Navigation documentation: https://developer.android.com/topic/libraries/architecture/adding-components#navigation as of June 29th 2018.

It says:

Blockquote Navigation classes are already in the androidx.navigation package, but currently depend on Support Library 27.1.1, and associated Arch component versions. Version of Navigation with AndroidX dependencies will be released in the future.

I guess for now you have no choice but waiting for a "future" release of the Navigation package.

For the time being, as suggested by @Levi Albuquerque, you need to use com.android.support:design:28.0.0-alpha3.
It should work the same way com.google.android.material:material:1.0.0-alpha3 does.