在 android 中动态切换 content/fragments
Dynamically switch content/fragments in android
我的应用目前使用导航主机(Android 架构组件导航)在片段之间切换。其中一个片段是一种更复杂的形式,我想 show/hide 一些内容取决于 button 1
或 button 2
是否被按下。
这是它的草图:
在 Android 中通常是如何完成的?我会在导航主机中显示的(蓝色背景)片段中添加第二个导航主机吗?我也在使用数据绑定,那么我是否必须为第二个导航主机内的片段使用第二个绑定对象,或者它们是否可以与 "main"(蓝色背景)片段共享一个绑定对象?
或者手动膨胀两个片段(一个用于 button1,一个用于 button2)会更好吗?但是,我将使用什么作为容器来托管它们?
由于您将导航组件用于主导航,从 "clean architecture" 的角度来看,最好也将它们用于 child Fragment
之间的导航.
您可以在 Fragment
中包含 NavHostFragment
。重要的是这次不要设置app:defaultNavHost="true"
。
我的示例应用有一个 FragmentB
和两个 children FragmentB1
和 FragmentB2
.
<androidx.fragment.app.FragmentContainerView
android:id="@+id/childNavHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/toggleGroup"
app:layout_constraintVertical_bias="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:navGraph="@navigation/child_navigation" />
child_navigation.xml
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/chlld__navigation.xml"
app:startDestination="@+id/dest_fragment_b1">
<fragment
android:id="@+id/dest_fragment_b1"
android:name="com.example.nestednavhostfragment.FragmentB1"
android:label="FragmentB1" />
<fragment
android:id="@+id/dest_fragment_b2"
android:name="com.example.nestednavhostfragment.FragmentB2"
android:label="FragmentB2" />
<action android:id="@+id/action_show_b1" app:destination="@+id/dest_fragment_b1"/>
<action android:id="@+id/action_show_b2" app:destination="@+id/dest_fragment_b2"/>
</navigation>
在 parent FragmentB
中使用 MaterialButtonToggleGroup
,您可以使用上面导航图中的 <action>
标签进行导航:
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:checkedButton="@+id/button1"
app:singleSelection="true"
app:layout_constraintBottom_toTopOf="@+id/childNavHostFragment"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.25">
<com.google.android.material.button.MaterialButton
style="?attr/materialButtonOutlinedStyle"
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checkable="true"
android:text="Show B1"
/>
<com.google.android.material.button.MaterialButton
style="?attr/materialButtonOutlinedStyle"
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Show B2"
/>
</com.google.android.material.button.MaterialButtonToggleGroup>
FragmentB
代码,注意你需要"find"正确的NavController
使用childNavHostFragment容器
class FragmentB : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_b_layout, container, false)
}
override fun onViewStateRestored(savedInstanceState: Bundle?) {
super.onViewStateRestored(savedInstanceState)
val childNavController = childNavHostFragment?.findNavController()
button1.setOnClickListener {childNavController?.navigate(R.id.action_show_b1)}
button2.setOnClickListener {childNavController?.navigate(R.id.action_show_b2)}
}
}
我的应用目前使用导航主机(Android 架构组件导航)在片段之间切换。其中一个片段是一种更复杂的形式,我想 show/hide 一些内容取决于 button 1
或 button 2
是否被按下。
这是它的草图:
在 Android 中通常是如何完成的?我会在导航主机中显示的(蓝色背景)片段中添加第二个导航主机吗?我也在使用数据绑定,那么我是否必须为第二个导航主机内的片段使用第二个绑定对象,或者它们是否可以与 "main"(蓝色背景)片段共享一个绑定对象?
或者手动膨胀两个片段(一个用于 button1,一个用于 button2)会更好吗?但是,我将使用什么作为容器来托管它们?
由于您将导航组件用于主导航,从 "clean architecture" 的角度来看,最好也将它们用于 child Fragment
之间的导航.
您可以在 Fragment
中包含 NavHostFragment
。重要的是这次不要设置app:defaultNavHost="true"
。
我的示例应用有一个 FragmentB
和两个 children FragmentB1
和 FragmentB2
.
<androidx.fragment.app.FragmentContainerView
android:id="@+id/childNavHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/toggleGroup"
app:layout_constraintVertical_bias="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:navGraph="@navigation/child_navigation" />
child_navigation.xml
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/chlld__navigation.xml"
app:startDestination="@+id/dest_fragment_b1">
<fragment
android:id="@+id/dest_fragment_b1"
android:name="com.example.nestednavhostfragment.FragmentB1"
android:label="FragmentB1" />
<fragment
android:id="@+id/dest_fragment_b2"
android:name="com.example.nestednavhostfragment.FragmentB2"
android:label="FragmentB2" />
<action android:id="@+id/action_show_b1" app:destination="@+id/dest_fragment_b1"/>
<action android:id="@+id/action_show_b2" app:destination="@+id/dest_fragment_b2"/>
</navigation>
在 parent FragmentB
中使用 MaterialButtonToggleGroup
,您可以使用上面导航图中的 <action>
标签进行导航:
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:checkedButton="@+id/button1"
app:singleSelection="true"
app:layout_constraintBottom_toTopOf="@+id/childNavHostFragment"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.25">
<com.google.android.material.button.MaterialButton
style="?attr/materialButtonOutlinedStyle"
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checkable="true"
android:text="Show B1"
/>
<com.google.android.material.button.MaterialButton
style="?attr/materialButtonOutlinedStyle"
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Show B2"
/>
</com.google.android.material.button.MaterialButtonToggleGroup>
FragmentB
代码,注意你需要"find"正确的NavController
使用childNavHostFragment容器
class FragmentB : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_b_layout, container, false)
}
override fun onViewStateRestored(savedInstanceState: Bundle?) {
super.onViewStateRestored(savedInstanceState)
val childNavController = childNavHostFragment?.findNavController()
button1.setOnClickListener {childNavController?.navigate(R.id.action_show_b1)}
button2.setOnClickListener {childNavController?.navigate(R.id.action_show_b2)}
}
}