将BottomNavigationDrawer连接到NavigationController的BottomAppBar [英] BottomAppBar with BottomNavigationDrawer connected to NavigationController

查看:136
本文介绍了将BottomNavigationDrawer连接到NavigationController的BottomAppBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在项目中使用新的Arch组件.简短说明我要达到的目标:

Hi I'm trying to use new arch components in my project. Short description what I want to achive:

  • 当用户使用MainFragment时,我想在BottomAppBar上显示导航图标(汉堡).用户可以单击导航图标并显示BottomNavigationDrawer
  • 当用户选择某个菜单项或单击MainFragment上的某项时,他被移至另一个片段,假设DebtDetailsFragment.然后,用NavigationController
  • 将汉堡包替换为后退箭头"
  • When user is on MainFragment I want to display navigation icon (Hamburger) on BottomAppBar. User is able to click navigation icon and display BottomNavigationDrawer
  • When user select some menu item, or click something on MainFragment he is moved to another fragment, let say DebtDetailsFragment. Then Hamburger should be replaced with 'Back arrow' by NavigationController

下面,我粘贴了我的MainActivity代码.当我在导航控制器上添加注释行时,汉堡包"图标可见,并且可以显示BottomNavigationDrawer.

Below I pasted my MainActivity code. When I comment line with navigation controller, the Hamburger icon is visible and BottomNavigationDrawer is able to display.

但是当我取消注释此行时,汉堡包消失了,因为NavigationControllerBottomNavigationDrawer中使用的NavigationView一无所知.我不使用DrawerLayout,所以管制员认为不需要汉堡包.

But when I uncomment this line, the Hamburger disappear because NavigationController knows nothing about NavigationView used in BottomNavigationDrawer. I don't use DrawerLayout, so controller thinks Hamburger is not needed.

方法setupWithNavController可以控制汉堡包"图标和后退箭头,但是我必须提供DrawerLayout作为我不使用的参数.

Method setupWithNavController can control Hamburger icon and back arrow, but I have to provide DrawerLayout as parameter which I don't use.

此方法的文档:

当您位于非根目录目标位置时,工具栏还将显示向上"按钮,而在根目录目标位置时,还将显示抽屉图标,从而在它们之间自动进行动画处理.单击导航图标时,此方法将调用[DrawerLayout.navigateUp].

The Toolbar will also display the Up button when you are on a non-root destination and the drawer icon when on the root destination, automatically animating between them. This method will call [DrawerLayout.navigateUp] when the navigation icon is clicked.

问题是,当NavigationControllerBottomAppBar连接但没有DrawerLayout时,如何显示汉堡图标?我将使用onOptionsItemSelected方法处理汉堡包单击自己.

So the question is, how to display Hamburger icon when NavigationController is connected with BottomAppBar but without DrawerLayout? I will handle hamburger click myself in onOptionsItemSelected method.

class MainActivity : BaseActivity() {

    @Inject
    lateinit var viewModelProvider: ViewModelProvider.Factory

    private val viewModel: MainActivityViewModel by lazy {
        ViewModelProviders.of(this, viewModelProvider).get(MainActivityViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(bottomAppBar)
        val navController = findNavController(R.id.main_nav_host_fragment)
        //bottomAppBar.setupWithNavController(navController)

        onDestroyDisposables += viewModel.uiStateObservable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(::render, Timber::e)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.bottomappbar_menu, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item?.itemId) {
            android.R.id.home -> {
                val bottomNavDrawerFragment = BottomNavigationDrawerFragment()
                bottomNavDrawerFragment.show(supportFragmentManager, bottomNavDrawerFragment.tag)
            }
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onSupportNavigateUp(): Boolean {
        return findNavController(R.id.main_nav_host_fragment).navigateUp()
    }
}


未设置导航控制器:


Without setted Navigation controller:

BottomNavigationDrawer

BottomNavigationDrawer

设置了NavigationController后-汉堡不可见.

With setted NavigationController - Hamburger invisible.

推荐答案

根据行为文档所示,向上"按钮应为显示在顶部Toolbar.

The BottomAppBar should never display an Up button as per the anatomy of the BottomAppBar - it should only ever display the drawer icon. As seen in the behavior documentation, the Up button should be displayed in a top Toolbar.

因此,您永远不应调用bottomAppBar.setupWithNavController(navController),而应使用已有的顶部Toolbar调用setupWithNavController(navController).

Therefore, you should never be calling bottomAppBar.setupWithNavController(navController), but instead calling setupWithNavController(navController) using whatever top Toolbar you have.

要设置您的BottomAppBar,您应该设置自己的抽屉图标和

To set up your BottomAppBar, you should instead set your own drawer icon and handle clicks on the drawer icon yourself.

DrawerArrowDrawable类可用于为您提供正确的抽屉图标:

The DrawerArrowDrawable class is available to give you a correct drawer icon:

val icon = DrawerArrowDrawable(bottomAppBar.context)
bottomAppBar.navigationIcon = icon

这篇关于将BottomNavigationDrawer连接到NavigationController的BottomAppBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆