如何使android导航主机与BottomNavigationView一起使用?

穆尔加德

我想使用新的导航图和BottomNavigationView我有以下依赖项:

// Navigation
implementation "androidx.navigation:navigation-fragment-ktx:$navVersion"
implementation "androidx.navigation:navigation-ui-ktx:$navVersion"
implementation "androidx.fragment:fragment:1.2.2"

// Material
implementation "com.google.android.material:material:$materialVersion"

请注意,implementation "androidx.fragment:fragment:1.2.2"它应该解决FragmentContainerView在导航图中未显示为主机的错误

这是我的简单导航图:

在此处输入图片说明

您在这里看到的第一个奇怪的事情是,即使我添加了可以修复它的依赖项,也没有任何内容显示为主机。

我的主要活动布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
        android:orientation="vertical"
        tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            app:defaultNavHost="true"
            app:navGraph="@navigation/navigation_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?colorPrimary"
            app:itemIconTint="@drawable/menu_navigation"
            app:itemTextColor="@drawable/menu_navigation"
            app:menu="@menu/menu_navigation" />

</LinearLayout>

和我的主要活动kotlin文件:

class MainActivity : AppCompatActivity() {

    private var navController: NavController? = null
    private var appBarConfiguration: AppBarConfiguration? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

        navController =
            (supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment).navController

        appBarConfiguration = AppBarConfiguration.Builder(
            setOf(
                R.id.main_fragment,
                R.id.recycler_fragment,
                R.id.map_fragment
            )
        ).build()

        setupActionBar()
        setupBottomNavigationMenu()
    }

    private fun setupActionBar() {
        NavigationUI.setupActionBarWithNavController(
            this,
            navController!!,
            appBarConfiguration!!
        )
    }

    private fun setupBottomNavigationMenu() {
        bottom_navigation?.let {
            NavigationUI.setupWithNavController(it, navController!!)
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_toolbar, menu)

        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val navigated = NavigationUI.onNavDestinationSelected(item, navController!!)

        return navigated || super.onOptionsItemSelected(item)
    }
}

这边还有菜单和工具栏:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
    <item
            android:id="@+id/destination_home"
            android:icon="@drawable/ic_home"
            android:title="@string/home"
            tools:layout="@layout/fragment_main" />
    <item
            android:id="@+id/destination_recycler"
            android:icon="@drawable/ic_list"
            android:title="@string/recycler"
            tools:layout="@layout/fragment_recycler" />
    <item
            android:id="@+id/destination_map"
            android:icon="@drawable/ic_map"
            android:title="@string/map"
            tools:layout="@layout/fragment_map" />
</menu>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
            android:id="@+id/destination_settings"
            android:icon="@drawable/ic_settings"
            android:title="@string/settings"
            app:showAsAction="ifRoom" />
</menu>

当我现在启动应用程序时,我会期望以下行为:

  1. 该应用程序启动并显示带有文本“ Home”的片段
  2. 该应用程序不会在工具栏中显示后退按钮
  3. When I navigate from "Home" to "Map" the toolbar would not show a back button in the toolbar
  4. When I navigate from "Home" to "Settings" the toolbar would show a back button in the toolbar
  5. When I navigate back from "Settings" the "Home" screen should appear
  6. When I navigate from "Map" to "Settings" the toolbar would show a back button in the toolbar
  7. When I navigate back from "Settings" the "Map" screen should now appear

What actually happens is:

  1. The app starts and shows the fragment with text "Home"
  2. The app shows a back button in the toolbar
  3. When I navigate from "Home" to "Map" the toolbar does show a back button in the toolbar
  4. When I navigate from "Home" to "Settings" the toolbar shows a back button in the toolbar
  5. When I navigate back from "Settings" the "Home" screen appears
  6. When I navigate from "Map" to "Settings" the toolbar shows a back button in the toolbar
  7. When I navigate back from "Settings" the instead "Map" the "Home" screen appears

So something is really messed up here with the navigation and I have no idea how to fix it.

ianhanniballake

Firstly, you are passing R.id.main_fragment, R.id.recycler_fragment, R.id.main_fragment to your AppBarConfiguration - besides including main_fragment twice, those names don't match up with the destination_home, destination_recycler, and destination_map in your menu XML. Since your clicking in your bottom nav bar works, those appear to be the correct destination IDs and you should change your AppBarConfiguration to use the same IDs:

appBarConfiguration = AppBarConfiguration.Builder(
        setOf(
            R.id.destination_home,
            R.id.destination_recycler,
            R.id.destination_map
        )
    ).build()

Or, if you're using the navigation-ui-ktx artifact, you'd use the top level AppBarConfiguration creator:

appBarConfiguration = AppBarConfiguration(setOf(
    R.id.destination_home,
    R.id.destination_recycler,
    R.id.destination_map))

As per the onNavDestinationSelected() Javadoc:

By default, the back stack will be popped back to the navigation graph's start destination. Menu items that have android:menuCategory="secondary" will not pop the back stack.

因此,可以预期的是,如果您在菜单中的“设置”屏幕中没有XML,则在您单击“后退”按钮时将使您返回到图形的起始目标。您想要更改菜单XML以包括该标志:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
            android:id="@+id/destination_settings"
            android:icon="@drawable/ic_settings"
            android:title="@string/settings"
            app:showAsAction="ifRoom"
            android:menuCategory="secondary" />
</menu>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使主机网络与docker swarm模式一起使用

如何使Tab键导航与弹出窗口一起使用?

将HOC与反应导航导航器一起使用

将Android Jetpack导航组件与Android BottomAppBar一起使用

如何使用远程PowerShell引导Windows主机以与Ansible一起使用?

如何设置 mysql 主机限制与 docker 容器一起使用

yum(在 centos 主机上)如何与需要 ssl 证书的代理一起使用

如何使用与远程主机一起发布的dotnet执行与框架相关的部署

如何让 SPA 导航与使用 innerHTML 作为内容的外部框架一起使用

如何在Android中将TextInputLayout与EditText一起使用

如何在Android中使ListView与ArrayAdapter一起使用

如何使Android BroadcastReceiver与AlarmManager一起使用?

如何在java Android中将IF与Button一起使用?

带有抽屉导航的Android主页按钮停止与旋转屏幕一起使用

如果路由在不同的包中,如何使导航与路由一起使用?

将导航组件与 DrawerLayout 一起使用时,如何修改 Toolbar/ActionBar?

如何使导航栏崩溃与动画汉堡包一起使用?

如何使用html,css和js一起包含侧边栏和导航栏

如何将响应式下拉导航插件与fullPage.js一起使用?

将导航组件与多个活动一起使用

将ToListAsync()与导航属性一起使用

测试与Jest一起使用反应导航的组件

Mapbox iOS导航SDK无法与SwiftUI一起使用

将FragmentContainerView与导航组件一起使用?

KnpMenuBundle无法与Bootstrap 4导航栏一起使用

在Flutter中将嵌套导航器与WillPopScope一起使用

如何配置两个不同的Web应用程序以在同一主机/端口上与Apache一起使用?

将BottomNavigationView与新的NavController一起使用时,是否有办法使片段保持活动状态?

在CoordinatorLayout中安排BottomNavigationView,FAB和FrameLayout并使其与Snackbar一起使用