导航组件 - 导航抽屉问题

问题描述 投票:1回答:1

其实我用我的应用程序中的新的架构组件,和我设置的导航组件。我有一个导航抽屉,我想与使用它。我已经设置它,但我遇到了一些问题:

1 - 抽屉不会关闭本身。菜单的工作原理,并导航到正确的地方,但它的导航后不关闭它。我不得不添加一个destinationChangedListener关闭它自己。

navController.addOnDestinationChangedListener { _, _, _ ->
    if (drawer_layout.isDrawerOpen(GravityCompat.START))
        drawer_layout.closeDrawer(GravityCompat.START)
}

codelabs,抽屉关闭本身,我真的不明白为什么。

2 - 向上按钮打开抽屉。当我浏览到一个非顶级片段,菜单图标变为向上的箭头,但是当我点击它,它会打开,而不是要回之前的目标抽屉菜单。

3,我想,我的菜单的一些项目,要在点击自定义。我所有的菜单上的按钮不用于导航到应用程序,我不知道我在哪里可以覆盖navController的行为,告诉它做什么时,他不能导航。

下面是相关的代码:

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/layout_app_bar"/>

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <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/app_navigation" />

        </androidx.coordinatorlayout.widget.CoordinatorLayout>

        <include layout="@layout/layout_navigation_view"/>

    </androidx.drawerlayout.widget.DrawerLayout>

</FrameLayout>

Layout_app_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:theme="@style/AppTheme.AppBarOverlay"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/language_main_color"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</com.google.android.material.appbar.AppBarLayout>

drawer_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">


    <group android:id="@+id/appAction" >
        <item
            android:id="@+id/bridgeFragment"
            android:icon="@drawable/ic_home"
            android:title="@string/home" />

        <item
            android:id="@+id/categoryFragment"
            android:icon="@drawable/ic_categories"
            android:title="@string/categories" />

        <item
            android:id="@+id/favoriteFragment"
            android:icon="@drawable/ic_favorite"
            android:title="@string/favorites" />

        <item
            android:id="@+id/unlimited"
            android:icon="@drawable/ic_unlimited"
            android:title="@string/dialog_ask_unlimited_title" />

        <!--<item
            android:id="@+id/settings"
            android:icon="@drawable/ic_settings"
            android:title="@string/settings" /> -->
    </group>


    <group android:id="@+id/globalAction">
        <item
            android:id="@+id/rate"
            android:icon="@drawable/ic_rate"
            android:title="@string/rate" />

        <item
            android:id="@+id/aboutFragment"
            android:icon="@drawable/ic_about_me"
            android:title="@string/about"/>
    </group>


</menu>

MainActivity.kt(setupMenus()和setupView函数()被调用的onCreate)

override fun setupView(){

        val host: NavHostFragment = supportFragmentManager
                .findFragmentById(R.id.nav_host_fragment) as NavHostFragment? ?: return

        val navController = host.navController

        val drawerLayout : DrawerLayout? = findViewById(R.id.drawer_layout)
        appBarConfiguration = AppBarConfiguration(
                setOf(R.id.bridgeFragment, R.id.categoryFragment, R.id.favoriteFragment, R.id.aboutFragment),
                drawerLayout)

        setupActionBarWithNavController(navController, appBarConfiguration)
        setupNavigationMenu(navController)

        navController.addOnDestinationChangedListener { _, _, _ ->
            if (drawer_layout.isDrawerOpen(GravityCompat.START))
                drawer_layout.closeDrawer(GravityCompat.START)
        }
    }

    fun updateHasMenu(hasMenu: Boolean) { this.hasMenu = hasMenu }

    private fun setupNavigationMenu(navController: NavController) {
        val sideNavView = findViewById<NavigationView>(R.id.nav_view)
        sideNavView?.setupWithNavController(navController)
    }

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

    override fun setupMenus(){
        setSupportActionBar(toolbar)
        supportActionBar?.let {
            it.setDisplayHomeAsUpEnabled(true)
            it.setHomeAsUpIndicator(R.drawable.ic_menu)
        }
    }
android android-architecture-components android-navigation-drawer android-architecture-navigation
1个回答
1
投票

这是你的第一个,第二个和第三个问题的整个工作代码。

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

private lateinit var navController: NavController

private lateinit var appBarConfiguration: AppBarConfiguration

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setSupportActionBar(toolbar)
    navController = findNavController(R.id.nav_controller_fragment)
    appBarConfiguration = AppBarConfiguration(navController.graph,drawer_layout)
    setupActionBarWithNavController(navController,appBarConfiguration)

    drawer()

    setupNavigationMenu()
    nav_view.setNavigationItemSelectedListener(this)
}

private fun drawer() {
    drawer_layout.addDrawerListener(object :DrawerLayout.DrawerListener{
        override fun onDrawerStateChanged(newState: Int) {
        }

        override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
        }

        override fun onDrawerClosed(drawerView: View) {
        }

        override fun onDrawerOpened(drawerView: View) {
        }
    })
}


private fun setupNavigationMenu() {
    nav_view.let {
        NavigationUI.setupWithNavController(it, navController)
    }
}

override fun onBackPressed() {
    if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
        drawer_layout.closeDrawer(GravityCompat.START)
    } else {
        super.onBackPressed()
    }
}

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

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    return when (item.itemId) {
        R.id.action_settings -> true
        else -> NavigationUI.onNavDestinationSelected(item,navController) || super.onOptionsItemSelected(item)

    }
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    drawer_layout.closeDrawer(GravityCompat.START)
    return when (item.itemId) {
        R.id.nav_gallery -> true
        else ->
        NavigationUI.onNavDestinationSelected(
            item,navController) || super.onOptionsItemSelected(item)
    }
}

override fun onSupportNavigateUp(): Boolean {
    return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}

}

这就是整个项目linkapk

© www.soinside.com 2019 - 2024. All rights reserved.