使用Navigation结合底部导航栏实现fragment切换

版权声明:本文为程序园中猿原创文章,转载请注明出处 https://blog.csdn.net/yinxing2008/article/details/88849954

简介

关于通过google官方提供的Navigation实现fragment切换,之前已经写了一篇《使用Navigation简化fragment切换》。本文继续深入,结合底部导航栏实现fragment切换.

使用方法

  1. build.gradle中添加依赖
implementation "android.arch.navigation:navigation-common-ktx:1.0.0"
implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0"
implementation "android.arch.navigation:navigation-runtime-ktx:1.0.0"
implementation "android.arch.navigation:navigation-ui-ktx:1.0.0"
  1. 添加navigation文件
    下面仅提供样例,完整代码请直接下载demo源代码
<?xml version="1.0" encoding="utf-8"?>
<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/home"
   app:startDestination="@+id/titleScreen">

   <fragment
       android:id="@+id/titleScreen"
       android:name="com.cxyzy.myapplication.FirstFragment"
       android:label="@string/first_page"
       tools:layout="@layout/fragment_first" />
</navigation>
  1. 添加menu文件bottom_nav.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/home"
        android:icon="@drawable/ic_first"
        android:contentDescription="@string/first_page"
        android:title="@string/first_page" />
    <item
        android:icon="@drawable/ic_second"
        android:contentDescription="@string/second_page"
        android:title="@string/second_page" />
</menu>
  1. MainActivity
class MainActivity : AppCompatActivity() {

    private var currentNavController: LiveData<NavController>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (savedInstanceState == null) {
            setupBottomNavigationBar()
        }
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        super.onRestoreInstanceState(savedInstanceState)
        setupBottomNavigationBar()
    }

    private fun setupBottomNavigationBar() {
        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav)

        val navGraphIds = listOf(R.navigation.navi_first, R.navigation.navi_second)

        // Setup the bottom navigation view with a list of navigation graphs
        val controller = bottomNavigationView.setupWithNavController(
                navGraphIds = navGraphIds,
                fragmentManager = supportFragmentManager,
                containerId = R.id.nav_host_container,
                intent = intent
        )

        // Whenever the selected controller changes, setup the action bar.
        controller.observe(this, Observer { navController ->
            setupActionBarWithNavController(navController)
        })
        currentNavController = controller
    }

    override fun onSupportNavigateUp(): Boolean {
        return currentNavController?.value?.navigateUp() ?: false
    }

    /**
     * Overriding popBackStack is necessary in this case if the app is started from the deep link.
     */
    override fun onBackPressed() {
        if (currentNavController?.value?.popBackStack() != true) {
            super.onBackPressed()
        }
    }
}
  1. 实现效果
    https://media.giphy.com/media/8Bl37OxaJ4nWs6eYrX/giphy.gif

源代码

https://gitee.com/cxyzy1/navigationDemo/tree/master/bottomNavigationView

附录

navigation官方介绍文档:https://codelabs.developers.google.com/codelabs/android-navigation/#0

另外一种在navigation中实现fragment复用的方法:
https://github.com/STAR-ZERO/navigation-keep-fragment-sample.git

安卓开发技术分享: https://blog.csdn.net/yinxing2008/article/details/84555061
更多技术总结好文,请关注:「程序园中猿」

猜你喜欢

转载自blog.csdn.net/yinxing2008/article/details/88849954
今日推荐