导航抽屉和底部导航项

时间:2020-07-02 18:12:54

标签: android navigation-drawer bottomnavigationview android-navigation-graph

Want to Create like this 是因为我使用了底部导航视图和导航抽屉。

我正在借助https://github.com/android/architecture-components-samples中的示例 NavigationAdvanceSample 处理带有导航图的底部导航视图。

现在我可以创建此视图,但是问题是,当单击导航抽屉项目时,我必须替换底部导航第5个项目片段中的新片段。

现在,当每个标签使用不同的导航图时,请帮助我。

setOnNavigationItemSelectedListener { item ->


    mainContainer.visibility = View.VISIBLE
    itemContainer.visibility = View.GONE


    // Don't do anything if the state is state has already been saved.
    if (fragmentManager.isStateSaved) {
        false
    } else {



        if(drawer.isDrawerOpen(GravityCompat.END)) {
            //drawer is close
            drawer.visibility = View.GONE
            drawer.closeDrawers()
        }
        val newlySelectedItemTag = graphIdToTagMap[item.itemId]

        if(newlySelectedItemTag != null){
            if (selectedItemTag != newlySelectedItemTag) {

                // Pop everything above the first fragment (the "fixed start destination")
                fragmentManager.popBackStack(firstFragmentTag,
                        FragmentManager.POP_BACK_STACK_INCLUSIVE)
                val selectedFragment = fragmentManager.findFragmentByTag(newlySelectedItemTag)
                        as NavHostFragment


                // Exclude the first fragment tag because it's always in the back stack.
                if (firstFragmentTag != newlySelectedItemTag) {
                    // Commit a transaction that cleans the back stack and adds the first fragment
                    // to it, creating the fixed started destination.


                    fragmentManager.beginTransaction()
                            .setCustomAnimations(
                                    R.anim.nav_default_enter_anim,
                                    R.anim.nav_default_exit_anim,
                                    R.anim.nav_default_pop_enter_anim,
                                    R.anim.nav_default_pop_exit_anim)
                            .attach(selectedFragment)
                            .setPrimaryNavigationFragment(selectedFragment)
                            .apply {
                                // Detach all other Fragments
                                graphIdToTagMap.forEach { _, fragmentTagItem ->
                                    if (fragmentTagItem != newlySelectedItemTag) {
                                        detach(fragmentManager.findFragmentByTag(firstFragmentTag)!!)
                                    }
                                }
                            }
                            .addToBackStack(firstFragmentTag)
                            .setReorderingAllowed(true)
                            .commit()
                }
                selectedItemTag = newlySelectedItemTag
                isOnFirstFragment = selectedItemTag == firstFragmentTag
                selectedNavController.value = selectedFragment.navController
                true
            } else {
                false
            }
        }else{
            Log.d("Tag", "Flag value before drawer$flag")

            drawer.visibility = View.VISIBLE
            drawer.openDrawer(GravityCompat.END)
            true
        }

    }
}

MainActivity.xml

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ripple="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.activity.TabActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/Medium"
    android:gravity="center"
    android:id="@+id/toolbar"
    android:orientation="horizontal"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    >

    <LinearLayout
        android:id="@+id/goBackButton"
        android:layout_width="wrap_content"
        android:background="?selectableItemBackground"
        android:layout_height="match_parent"
        android:layout_weight=".15"
        android:visibility="invisible"
        android:gravity="center_horizontal|center_vertical"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/inzeView10"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@mipmap/back"
            android:visibility="visible" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/switch_child"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight=".7"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="horizontal">

        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/user_imageview"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:visibility="gone"
            ripple:failureImage="@mipmap/child_pic"
            ripple:placeholderImage="@mipmap/child_pic"
            ripple:roundAsCircle="true" />

        <TextView
            android:id="@+id/fragmentName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:maxLength="20"
            android:padding="3dp"
            android:textColor="@color/white"
            android:textSize="14dp" />

        <ImageView
            android:id="@+id/switchChildDropdown"
            android:layout_width="14dp"
            android:layout_height="15dp"
            android:layout_marginTop="2dp"
            android:src="@mipmap/white_drop_down"
            android:visibility="gone" />
    </LinearLayout>

    <RelativeLayout
        android:orientation="horizontal"
        style="@style/SelectableItemBackground"
        android:layout_width="0dp"
        android:layout_weight=".1"
        android:gravity="center"
        android:visibility="gone"
        android:id="@+id/toolbarRightLayoutFirst"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:visibility="visible"
            android:src="@mipmap/setting_white"
            android:id="@+id/toolbarRightImage1"
            />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/toolbarRightLayout"
        style="@style/SelectableItemBackground"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight=".1"
        android:visibility="visible"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/toolbarRightImage"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@mipmap/group_icon"
            android:visibility="gone" />

        <TextView
            android:id="@+id/toolbarRightText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:gravity="center"
            android:maxLength="20"
            android:visibility="gone"
            android:textColor="@color/white"
            android:textSize="14dp" />
    </RelativeLayout>


</LinearLayout>



<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    android:layout_marginBottom="76dp"
    app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolbar"
    app:layout_constraintEnd_toEndOf="parent"
    >

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/add_child_back"
        >

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/nav_host_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/nav_host_container_item"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


    </androidx.constraintlayout.widget.ConstraintLayout>


    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:background="@drawable/drawer_item_bg"
        android:fitsSystemWindows="true"
        app:menu="@menu/side_drawer">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ListView
                android:id="@+id/lst_menu_items"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:divider="@color/dim_back"
                android:dividerHeight="1dp"

                />
        </LinearLayout>
    </com.google.android.material.navigation.NavigationView>


</androidx.drawerlayout.widget.DrawerLayout>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:layout_width="76dp"
    android:layout_height="80dp"
    android:layout_marginBottom="4dp"
    android:visibility="gone"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="@+id/bottom_navigation"
    app:layout_constraintStart_toStartOf="@+id/bottom_navigation" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:foreground="?attr/selectableItemBackground"

    app:itemBackground="@drawable/tab_color"
    app:itemIconTint="@color/white"
    app:itemTextColor="@color/white"
    app:labelVisibilityMode="labeled"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:menu="@menu/bottom_navigation_menu" />
    </androidx.constraintlayout.widget.ConstraintLayout>

如果有人知道该怎么做,请帮忙。

预先感谢

0 个答案:

没有答案