是否可以使用底部导航视图和Navigation组件传递和访问片段中的参数?
我使用的是一个活动带多个片段的方法,我的顶级片段需要一个参数(通常通过newInstance生成的方法完成)。我看了导航组件开发者指南和codelab,但它只提到了使用safeargs和在目的地和操作中添加参数标记。
这是我的导航图:
<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>
主要活动:
override fun onCreate(savedInstanceState: Bundle?) {
...
val navController = Navigation.findNavController(this,
R.id.nav_host_fragment)
bottom_navigation.setupWithNavController(navController)
....
}
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>
主页片段
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val argument = //TODO access argument here
...
}
4条答案
按热度按时间xuo3flqw1#
如果我理解正确的话,您希望将参数传递给与菜单项绑定的目标。尝试在你的activity onCreate方法中使用'OnDestinationChangedList',类似这样:
更新:
如果你想让你的起始目标接收默认参数,那么实现应该是不同的。首先,从“NavHostFragment”xml标记中删除“app:navGraph="@navigation/nav_graph”“。
然后,在你的activity onCreate中,你需要放大图:
然后将参数添加到graph(此参数将附加到start destination)
然后将图形附加到NavController:
现在,您的第一个目的地应该接收附加的参数。
guicsvcw2#
正确的方法是在目的地使用
<argument>
块。这将自动使用默认值填充Fragment的参数,而不需要任何额外的代码。从Navigation 1.0.0-alpha 09开始,无论您是否使用安全Args Gradle插件,这都是正确的。
56lgkhnf3#
默认值对我来说是不可用的,因为我有动态菜单项,可以有多个相同的目标,不同的参数。(从服务器更改)
实现BottomNavigationView. OnNavigationItemSelectedView:
要使用它,您将通过替换侦听器来接管导航。这里调用的顺序很重要:
q0qdq0h24#
我的参数已经在导航图xml中定义了,不知道为什么,但@Alex的答案对我不起作用,相反,我这样做了(从destinationChangedList):
注意,我使用这个来给予一个特定的响应时,深链接到一个特定的导航栏标签。默认情况下,我还需要这样做(以避免重复使用同一个bundle和再次发生深层链接处理):