如何使用底部导航视图和Android导航组件将参数传递给片段

如何使用底部导航视图和Android导航组件将参数传递给片段

本文介绍了如何使用底部导航视图和Android导航组件将参数传递给片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用底部导航视图和Navigation组件在片段中传递和访问参数?

Is it possible to pass and access arguments in a fragment using a bottom navigation view and the Navigation component?

我正在使用一个包含多个片段的活动,其中我的顶层片段需要一个参数(通常通过newInstance生成的方法完成).我看过了Navigation组件开发人员指南和代码实验室,但只提到了使用safeargs并在目标和操作中添加了参数标签.

I'm using a one activity with many fragments approach where my top level fragment requires an argument(Usually done via the newInstance generated method). I've had a look at the Navigation component developer guide and the codelab but it only mentions using safeargs and adding argument tags in the destinations and actions.

这是我的导航图:

<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    app:startDestination="@id/homeFragment">

    <fragment android:id="@+id/homeFragment"
          android:name="uk.co.homeready.homeready.HomeFragment"
          android:label="fragment_home"
          tools:layout="@layout/fragment_home">
          <!--Do I create an argument block here?-->
    </fragment>

    <fragment android:id="@+id/calculatorFragment"
          android:name="uk.co.homeready.homeready.CalculatorFragment"
          android:label="fragment_calculator"
          tools:layout="@layout/fragment_calculator"/>

    <fragment android:id="@+id/resourcesFragment"
          android:name="uk.co.homeready.homeready.ResourcesFragment"
          android:label="fragment_resources"
          tools:layout="@layout/fragment_resources"/>

</navigation>

底部导航视图菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/homeFragment"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/title_home"/>

    <item
        android:id="@+id/calculatorFragment"
        android:icon="@drawable/ic_baseline_attach_money_24px"
        android:title="@string/title_calculator"/>

    <item
        android:id="@+id/resourcesFragment"
        android:icon="@drawable/ic_baseline_library_books_24px"
        android:title="@string/title_resources"/>

</menu>

MainActivity:

MainActivity:

override fun onCreate(savedInstanceState: Bundle?) {
        ...
        val navController = Navigation.findNavController(this,
        R.id.nav_host_fragment)
        bottom_navigation.setupWithNavController(navController)
        ....
}

activity_main.xml

activity_main.xml

<android.support.constraint.ConstraintLayout>
    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:layout_constraintBottom_toTopOf="@id/bottom_navigation"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        app:menu="@menu/bottom_navigation"/>

</android.support.constraint.ConstraintLayout>

HomeFragment

HomeFragment

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val argument = //TODO access argument here
    ...
}

推荐答案

如果我对您的理解正确,则希望将参数传递给与菜单项相关联的目标.尝试在活动onCreate方法中使用"OnDestinationChangedListener",如下所示:

If I understood you correctly, you want to pass arguments to destinations that is tied to menu items. Try to use 'OnDestinationChangedListener' inside your activity onCreate method, something like this:

navController.addOnDestinationChangedListener { controller, destination, arguments ->
        when(destination.id) {
            R.id.homeFragment -> {
                val argument = NavArgument.Builder().setDefaultValue(6).build()
                destination.addArgument("Argument", argument)
            }
        }
    }

更新:

如果您希望起始目的地将接收默认参数,则实现应有所不同.首先,删除"app:navGraph =" @ navigation/nav_graph""从您的"NavHostFragment" xml标签中获取.

If you want that your start destination will receive default arguments the implementation should be different.First, remove 'app:navGraph="@navigation/nav_graph"' from your 'NavHostFragment' xml tag.

然后,在活动onCreate内,需要对图形进行充气:

Then, inside your activity onCreate you need to inflate the graph:

 val navInflater = navController.navInflater
 val graph = navInflater.inflate(R.navigation.nav_graph)

然后将您的参数添加到图形(此参数将附加到起始目标位置)

Then add your arguments to graph(this arguments will be attached to start destination)

val navArgument1=NavArgument.Builder().setDefaultValue(1).build()
val navArgument2=NavArgument.Builder().setDefaultValue("Hello").build()
graph.addArgument("Key1",navArgument1)
graph.addArgument("Key2",navArgument2)

然后将图形附加到NavController:

Then attach the graph to NavController:

navController.graph=graph

现在,您的第一个目的地应该收到附加的参数.

Now your first destination should receive the attached arguments.

这篇关于如何使用底部导航视图和Android导航组件将参数传递给片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 03:20