带有多个堆栈和导航组件的DrawerLayout。

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

我正在使用导航组件,在我的一个片段中,我有一个带有多个堆栈的抽屉布局。

导航组件

Splash -> Login -> Home(drawer layout)

在Home中,我想使用抽屉布局和2个碎片堆栈。

Home (Search Nav Graph) -> Search -> S1 -> S2
Home (Profile Nav Graph) -> Profile -> P1

首页碎片

class HomeFragment : BaseFragment() {
    private var _binding: FragmentHomeBinding? = null
    private val binding get() = _binding!!
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val appBarConfiguration = AppBarConfiguration(setOf(R.navigation.search_nav_graph, R.navigation.profile_nav_graph), binding.drawerLayout)
        binding.collapsingToolbarLayout.setupWithNavController(binding.toolbar, findNavController(), appBarConfiguration)
        return binding.root
    }
}

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/tall_toolbar_height">
            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleGravity="top"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"/>
            </com.google.android.material.appbar.CollapsingToolbarLayout>
        </com.google.android.material.appbar.AppBarLayout>

        <!-- The main content view where fragments are loaded -->
        <FrameLayout
            android:id="@+id/flContent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    <!-- Container for contents of drawer - use NavigationView to make configuration easier -->
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="false">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <include layout="@layout/drawer_header" />
            <include layout="@layout/drawer_menu" />
        </LinearLayout>
    </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

我想办法是用 flContent 但我没有找到根据 文件

android androidx android-architecture-navigation
1个回答
1
投票

我终于意识到,我可以只使用 childFragmentManager

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val busquedaNavHostFragment = NavHostFragment.create(R.navigation.search)
        val perfilNavHostFragment = NavHostFragment.create(R.navigation.profile)
        childFragmentManager.beginTransaction()
                .add(R.id.flContent, busquedaNavHostFragment, busquedaNavHostFragment.javaClass.name)
                .add(R.id.flContent, perfilNavHostFragment, perfilNavHostFragment.javaClass.name)
                .hide(busquedaNavHostFragment)
                .show(perfilNavHostFragment)
                .commitNow()

        binding.navView.findViewById<LinearLayout>(R.id.ll_perfil).setOnClickListener {
            binding.drawerLayout.close()
            childFragmentManager.beginTransaction()
                    .show(perfilNavHostFragment)
                    .hide(busquedaNavHostFragment)
                    .commit()
        }
        binding.navView.findViewById<LinearLayout>(R.id.ll_busqueda).setOnClickListener {
            binding.drawerLayout.close()
            childFragmentManager.beginTransaction()
                    .show(busquedaNavHostFragment)
                    .hide(perfilNavHostFragment)
                    .commit()
        }
        return binding.root
    }
© www.soinside.com 2019 - 2024. All rights reserved.