问题描述
我创建了 Navigation Drawer Activity ,作为 Navigation Drawer Activity 的新格式,根据新的Android架构,我使用 导航组件 结构.
I have created Navigation Drawer Activity, As updated new format of Navigation Drawer Activity, As per new Android architecture, I got it with Navigation Component structure.
带有 NavController
和 NavigationUI
的 NavigationView
代码,如下所示,当我单击任何导航项时,这是打开的片段.
The NavigationView
code with NavController
and NavigationUI
as below which is opening fragment when I click on any navigation item.
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_profile, R.id.nav_privacy_policy,
R.id.nav_terms, R.id.nav_contact_us, R.id.nav_share, R.id.nav_send)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
这是针对 nav_host_fragment
:
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
使用此 navigation/mobile_navigation.xml
<?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/mobile_navigation"
app:startDestination="@+id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.sohamerp.marsremedies.fragment.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/nav_profile"
android:name="com.sohamerp.marsremedies.fragment.ProfileFragment"
android:label="@string/menu_my_profile"
tools:layout="@layout/fragment_profile" />
<fragment
android:id="@+id/nav_privacy_policy"
android:name="com.sohamerp.marsremedies.fragment.PrivacyPolicyFragment"
android:label="@string/menu_privacy_policy"
tools:layout="@layout/fragment_privacy_policy" />
<fragment
android:id="@+id/nav_terms"
android:name="com.sohamerp.marsremedies.fragment.TermsConditionFragment"
android:label="@string/menu_terms"
tools:layout="@layout/fragment_terms_condition" />
<fragment
android:id="@+id/nav_contact_us"
android:name="com.sohamerp.marsremedies.fragment.ContactUsFragment"
android:label="@string/menu_contact_us"
tools:layout="@layout/fragment_terms_condition" />
</navigation>
我想做什么:
现在,我想在Fragment调用时将某些值作为bundle(参数)传递给Fragment.
What I want to do:
Now I want to pass some values as bundle (arguments) in Fragment when its called.
场景:我有两个片段 PrivacyPolicyFragment
和 TermsConditionsFragment
,在这两个片段中,我只是相应地在WebView中打开链接.因此,当我单击 Privacy Policy
的菜单项时,我将传递与此相关的链接.
Scenario: I have two fragment PrivacyPolicyFragment
and TermsConditionsFragment
, In both fragments, I am just opening link inside WebView accordingly. So When I click on menu item of Privacy Policy
, I will pass link related to same.
在此新结构 navigation/mobile_navigation.xml
开头的片段中,如何传递参数?
In this new structure navigation/mobile_navigation.xml
opening fragments, How can I pass arguments?
有帮助吗?
推荐答案
所以我忘了通过以下链接:定义目标参数
So I forgot to go through this link : Define Destination Arguments
但是这个答案对像我这样的所有懒惰人很有帮助:
But this answer helpful to all lazy peoples like me:
在项目级别 build.gradle 中添加依赖项:
Add dependency in project level build.gradle:
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.1.0"
在应用程序级别 build.gradle 中应用插件:
Apply plugin in app level build.gradle:
apply plugin: "androidx.navigation.safeargs"
使用XML:预定义(静态)值:
在导航/navigation/mobile_navigation.xml 的xml文件中,如下所示声明 argument
标记,或者您可以通过此链接:
Using XML: predefined (static) value:
In xml file of navigation /navigation/mobile_navigation.xml declare argument
tag as below or you can design through this link:
<fragment
android:id="@+id/nav_privacy_policy"
android:name="com.sohamerp.marsremedies.fragment.PrivacyPolicyFragment"
android:label="@string/menu_privacy_policy"
tools:layout="@layout/fragment_privacy_policy" >
<argument
android:name="privacyPolicyLink"
app:argType="string"
android:defaultValue="http://sohamerp.com/avo/avo_privacy_policy.html"/>
</fragment>
<fragment
android:id="@+id/nav_terms"
android:name="com.sohamerp.marsremedies.fragment.PrivacyPolicyFragment"
android:label="@string/menu_terms"
tools:layout="@layout/fragment_terms_condition" >
<argument
android:name="privacyPolicyLink"
app:argType="string"
android:defaultValue="http://sohamerp.com/avo/avo_privacy_policy.html"/>
</fragment>
现在,您必须像这样在Fragment中编写代码:
Now you have to write code in your Fragment like:
if(getArguments() != null) {
// The getPrivacyPolicyLink() method will be created automatically.
String url = PrivacyPolicyFragmentArgs.fromBundle(getArguments()).getPrivacyPolicyLink();
}
希望它会对您有帮助.
这篇关于Android导航组件:在片段中传递值(参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!