Navigation简单使用
针对Jitpack
组件中 Navigation
的简单使用说明。
添加依赖
需要在对应模块层级的build.gradle
文件中添加依赖:
//版本指定为当时的最新稳定版本
ext.navigationVersion = "2.0.0"
dependencies {
//...
implementation "androidx.navigation:navigation-fragment-ktx:$rootProject.navigationVersion"
implementation "androidx.navigation:navigation-ui-ktx:$rootProject.navigationVersion"
}
创建布局文件
在需要使用navigation
的布局中,创建对应的xml资源文件。其中Navigation
中最重要的组成部分有三个。
NavHostFragment
navigation组件生效的fragment容器,指定NavHostFragment
后Navigation
才能实现后续的效果。
<androidx.constraintlayout.widget.ConstraintLayout
...>
<fragment
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/login_navigation"
app:defaultNavHost="true"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
这里有几个属性需要进行说明:
属性 | 说明 | 备注 |
---|---|---|
android:name | 值必须是androidx.navigation.fragment.NavHostFragment,声明这是一个NavHostFragment | 可以自己实现NavHostFragment |
app:navGraph | 存放的是第二步建好导航的资源文件,也就是确定了NavigationGraph | |
app:defaultNavHost | 是否与系统的返回按钮相关联,默认true,表示关联 |
NavigationGrap
navigation事件的描述文件。
- 创建基础目录:资源文件res目录下创建navigation目录 -> 右击navigation目录New一个Navigation resource file。
- 创建一个Destination,如果说Navigation是我们的导航工具,Destination是我们的目的地,
<navigation
...>
<fragment
...
/>
<fragment
android:id="@+id/welcome"
>
<action
android:id="@+id/action_welcome_to_login"
app:destination="@id/login"/>
<action
android:id="@+id/action_welcome_to_register"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right"
app:destination="@id/register"/>
</fragment>
<fragment
android:id="@+id/register"
...
>
<argument
android:name="EMAIL"
android:defaultValue="2005@qq.com"
app:argType="string"/>
</fragment>
</navigation>
NavController
实现NavigationGrap中的事件。参考NavigationGrap
中的事件,在对应的app:startDestination:xxFragment中实现这些事件。
更多
Navigation
可以绑定menus
、drawers
和bottom navigation
,这里我们以bottom navigation
为例。
布局文件:
<LinearLayout
...>
<fragment
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
app:navGraph="@navigation/main_navigation"
app:defaultNavHost="true"
android:layout_height="0dp"
android:layout_weight="1"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
app:itemIconTint="@color/colorAccent"
app:itemTextColor="@color/colorPrimary"
app:menu="@menu/menu_main"/>
</LinearLayout>
XxActivity中的处理:
class MainActivity : AppCompatActivity() {
lateinit var bottomNavigationView: BottomNavigationView
override fun onCreate(savedInstanceState: Bundle?) {
//...
val host: NavHostFragment = supportFragmentManager.findFragmentById(R.id.my_nav_host_fragment) as NavHostFragment
val navController = host.navController
initWidget()
initBottomNavigationView(bottomNavigationView,navController)
}
private fun initBottomNavigationView(bottomNavigationView: BottomNavigationView, navController: NavController) {
bottomNavigationView.setupWithNavController(navController)
}
private fun initWidget() {
bottomNavigationView = findViewById(R.id.navigation_view)
}
}