mon*_*key 6 android android-architecture-components android-architecture-navigation
这是一个简单的导航设置示例,经过一段时间的研究,我无法使用导航组件库。
顶部的粘性片段和底部的片段在它们自己的导航图中,这里是 main_activity.xml:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/nav_sticky_top"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/navigation_graph_sticky_top" />
<fragment
android:id="@+id/navigation_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/nav_sticky_top"
app:navGraph="@navigation/navigation_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
当我按“导航到同级片段”时,它导航到底部导航图中的同级片段,这是正确的,结果:
这是 navigation_graph.xml:
<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/navigation_graph.xml"
app:startDestination="@id/blankFragment1">
<fragment
android:id="@+id/blankFragment1"
android:name="com.example.myapplication.BlankFragment1"
android:label="fragment_blank_fragment1"
tools:layout="@layout/fragment_blank_fragment1">
<action
android:id="@+id/action_blankFragment1_to_siblingFragment"
app:destination="@id/siblingFragment" />
</fragment>
<fragment
android:id="@+id/siblingFragment"
android:name="com.example.myapplication.SiblingFragment"
tools:layout="@layout/fragment_sibling_fragment" />
</navigation>
Run Code Online (Sandbox Code Playgroud)
现在我想实现“导航到全屏片段”按钮,该按钮应该导航到一个位于单独的第三导航图中的全屏片段,并且应该位于粘性片段导航图上方和下方的导航图。如何正确实现这一目标?我的意思是没有像将顶部导航图片段的可见性设置为 GONE 以及在底部导航图中导航到全屏片段这样的技巧。
更新:我不再推荐这种方法。对于小型应用程序,它可能对您有用,但是当我尝试在不同NavHostFragments. 我认为最简单的方法是在顶级布局中隐藏片段以允许全屏视图的“黑客”。您可以添加一个addOnDestinationChangedListener以全屏收听目的地,只需隐藏所需的片段。
原始答案:我在我的应用程序中这样做并且效果很好。您应该将所有当前片段包装在一个带有main_nav_graph.xml. 例如:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/main_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/main_nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
然后您的全屏片段导航将如下所示:
<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/main_nav_graph.xml"
app:startDestination="@id/main_fullscreen">
<fragment
android:id="@+id/main_fullscreen"
android:name="com.example.myapplication.MainFullscreen" >
<action
android:id="@+id/action_main_fullscreen_to_fullscreen2"
app:destination="@id/fullscreen2" />
</fragment>
<fragment
android:id="@+id/fullscreen2"
android:name="com.example.myapplication.Fullscreen2" />
</navigation>
Run Code Online (Sandbox Code Playgroud)
MainFullscreen 的布局与您提供的 ConstraintLayout 类似。只需确保添加app:defaultNavHost="true"到所有子 NavHostFragments。
然后当我想从你的 blankFragment1 导航到 fullscreen2 时,我称之为:
Navigation.findNavController(activity!!, R.id.main_host_fragment).navigate(R.id.action_main_fullscreen_to_fullscreen2)
Run Code Online (Sandbox Code Playgroud)
当然,如果导航图没有真正连接,除了通过同一活动之外,导航图没有多大意义,但我只是喜欢图在导航编辑器中为您提供的应用程序的漂亮图片:)
| 归档时间: |
|
| 查看次数: |
3050 次 |
| 最近记录: |