JetPack-Navigation

  1. build.grade下引入
    nav_version = "2.3.0-alpha01"

java
implementation "android.arch.navigation:navigation-fragment:$nav_version"
implementation "android.arch.navigation:navigation-ui:$nav_version"
kotilin
implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
implementation "android.arch.navigation:navigation-ui-ktx:$nav_version"

  1. 右键项目 -> new->Android Resource Directory->(resource type) navigation
    创建 xxx.xml

  2. 在主activity 布局文件中引入
    android:id="@+id/fragment_nav_host"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph"/>

4.在nav_graph.xml 的视图 design中添加创建的fragment,并连接 跳转关系, 自动生成代码,产生
action_id

  1. 页面跳转点击实现:
    NavHostFragment.findNavController(this).navigate(R.id.action_id)
    NavHostFragment.findNavController(this).navigate(R.id.action_id, bundle)

你可能感兴趣的:(JetPack-Navigation)