【问题标题】:How to combine Drawerlayout with BottomNavigationView如何将 Drawerlayout 与 BottomNavigationView 结合使用
【发布时间】:2021-12-11 07:31:13
【问题描述】:

构建成功,但导航控制器不起作用。当我点击底部导航项时,对应的fragment不会覆盖宿主fragment。

MainActivity

class MainActivity : AppCompatActivity(){
    private lateinit var binding: ActivityMainBinding
    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var navController: NavController
    private var isInitial = true
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        initView()
    }

    private fun initView(){
        navController = findNavController(R.id.nav_host_main)
        binding.bottomNavMain.setupWithNavController(navController)
        binding.leftNavMain.setupWithNavController(navController)
        appBarConfiguration = AppBarConfiguration(setOf(R.id.dish_Fragment, R.id.cart_Fragment, R.id.home_Fragment, R.id.order_Fragment, R.id.friend_Fragment), binding.drawerLayoutMain)
        setupActionBarWithNavController(navController, appBarConfiguration)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return item.onNavDestinationSelected(navController) ||
                super.onOptionsItemSelected(item)
    }

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

Main xml

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

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/container_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottom_nav_main"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?android:attr/windowBackground"
                app:layout_constraintBottom_toBottomOf="parent"
                app:menu="@menu/bottom_navigation" />

            <fragment
                android:id="@+id/nav_host_main"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:defaultNavHost="true"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:navGraph="@navigation/mobile_navigation" />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/left_nav_main"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:menu="@menu/activity_main_drawer" />
    </androidx.drawerlayout.widget.DrawerLayout>
</layout>

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/mobile_navigation"
    app:startDestination="@id/homeFragment">

    <fragment
        android:id="@+id/nav_home"
        android:name="com.example.shereats.view.ui.home.MHomeFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/nav_gallery"
        android:name="com.example.shereats.view.ui.gallery.GalleryFragment"
        android:label="@string/menu_gallery"
        tools:layout="@layout/fragment_gallery" />

    <fragment
        android:id="@+id/nav_slideshow"
        android:name="com.example.shereats.view.ui.slideshow.SlideshowFragment"
        android:label="@string/menu_slideshow"
        tools:layout="@layout/fragment_slideshow" />
    <fragment
        android:id="@+id/dish_Fragment"
        android:name="com.example.shereats.view.fragment.DishFragment"
        android:label="dish_fragment"
        tools:layout="@layout/dish_fragment" />
    <fragment
        android:id="@+id/cart_Fragment"
        android:name="com.example.shereats.view.fragment.CartFragment"
        android:label="cart_fragment"
        tools:layout="@layout/cart_fragment" />
    <fragment
        android:id="@+id/home_Fragment"
        android:name="com.example.shereats.view.fragment.HomeFragment"
        android:label="home_fragment"
        tools:layout="@layout/home_fragment" />
    <fragment
        android:id="@+id/order_Fragment"
        android:name="com.example.shereats.view.fragment.OrderFragment"
        android:label="order_fragment"
        tools:layout="@layout/order_fragment" />
    <fragment
        android:id="@+id/friend_Fragment"
        android:name="com.example.shereats.view.fragment.FriendFragment"
        android:label="friend_fragment"
        tools:layout="@layout/friend_fragment" />

</navigation>

【问题讨论】:

    标签: android android-jetpack drawerlayout android-jetpack-navigation


    【解决方案1】:

    在集成Jetpack Navigation时,我们需要保持目的地片段的id与menu.xml一致

            appBarConfiguration = AppBarConfiguration(setOf(R.id.dishFragment, R.id.cartFragment, R.id.homeFragment, R.id.orderFragment, R.id.friendFragment), binding.drawerLayoutMain)
    
    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:id="@+id/dishFragment"
            android:icon="@drawable/ic_baseline_local_cafe_24"
            android:title="@string/nav_text_one"/>
        <item
            android:id="@+id/cartFragment"
            android:icon="@drawable/ic_baseline_shopping_cart_24"
            android:title="@string/nav_text_two"/>
        <item
            android:id="@+id/homeFragment"
            android:icon="@drawable/ic_baseline_home_24"
            android:title="@string/nav_text_three"/>
        <item
            android:id="@+id/orderFragment"
            android:icon="@drawable/ic_baseline_assignment_24"
            android:title="@string/nav_text_four"/>
        <item
            android:id="@+id/friendFragment"
            android:icon="@drawable/ic_baseline_group_24"
            android:title="@string/nav_text_five"/>
    </menu>
    
    
    <?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/mobile_navigation"
        app:startDestination="@id/homeFragment">
    
        <fragment
            android:id="@+id/nav_home"
            android:name="com.example.shereats.view.ui.home.MHomeFragment"
            android:label="@string/menu_home"
            tools:layout="@layout/fragment_home" />
    
        <fragment
            android:id="@+id/nav_gallery"
            android:name="com.example.shereats.view.ui.gallery.GalleryFragment"
            android:label="@string/menu_gallery"
            tools:layout="@layout/fragment_gallery" />
    
        <fragment
            android:id="@+id/nav_slideshow"
            android:name="com.example.shereats.view.ui.slideshow.SlideshowFragment"
            android:label="@string/menu_slideshow"
            tools:layout="@layout/fragment_slideshow" />
        <fragment
            android:id="@+id/dishFragment"
            android:name="com.example.shereats.view.fragment.DishFragment"
            android:label="dish_fragment"
            tools:layout="@layout/dish_fragment" />
        <fragment
            android:id="@+id/cartFragment"
            android:name="com.example.shereats.view.fragment.CartFragment"
            android:label="cart_fragment"
            tools:layout="@layout/cart_fragment" />
        <fragment
            android:id="@+id/homeFragment"
            android:name="com.example.shereats.view.fragment.HomeFragment"
            android:label="home_fragment"
            tools:layout="@layout/home_fragment" />
        <fragment
            android:id="@+id/orderFragment"
            android:name="com.example.shereats.view.fragment.OrderFragment"
            android:label="order_fragment"
            tools:layout="@layout/order_fragment" />
        <fragment
            android:id="@+id/friendFragment"
            android:name="com.example.shereats.view.fragment.FriendFragment"
            android:label="friend_fragment"
            tools:layout="@layout/friend_fragment" />
    
    </navigation>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-20
      • 2020-06-22
      • 2018-07-12
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      相关资源
      最近更新 更多