我目前正在使用新的(对我来说)导航组件编写应用程序。我已经用一个导航图来浏览我的应用程序的基础知识,我有一个带有BottomNavigationView的片段,其中有3个单独的片段,我设法将其更新为使用导航组件(到目前为止我的问题)使用具有与导航项匹配的ID的菜单。我以前所有使用newInstance方法将捆绑包传递给onCreate的片段现在显然都没有使用,但是我仍然需要将捆绑包传递给我的片段。
由于片段是隐式创建的,因此我无法找到完成此操作的任何示例。
我的代码的结构为ClientFragment,这是导航抽屉等的主机片段;
class ClientFragment : Fragment() {
private val viewModel: ClientViewModel by viewModel()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_client, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.client = arguments?.getParcelable(ARG_CLIENT)!!
toolbar_client.title = viewModel.client.name
toolbar_client.setNavigationOnClickListener { Navigation.findNavController(view).navigateUp() }
}
}
该类以前使用onView侦听器来监视我的片段,并带有一个newInstance方法,该方法使用viewModel.client工具。
我在nav_graph中的片段都是相似的。第一个片段;
class ClientDetailsFragment : Fragment() {
private val viewModel: ClientViewModel by viewModel()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_client_details, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// viewModel.client = arguments?.getParcelable(ARG_CLIENT)!!
initClientDetails()
}
private fun initClientDetails() {
// text_client_details_name.text = viewModel.client.name
// text_client_details_account_number.text = viewModel.client.accountNumber
// text_client_details_mobile_number.text = viewModel.client.mobileNumber
// text_client_details_landline_number.text = viewModel.client.landlineNumber
// text_client_details_email.text = viewModel.client.email
// text_client_details_address.text = "NOT YET IMPLEMENTED"
//
// text_client_description_body.text = viewModel.client.description
// text_client_system_details_body.text = viewModel.client.systemDetails
}
}
应用程序在注释行上崩溃;
// viewModel.client = arguments?.getParcelable(ARG_CLIENT)!!
我的导航图和菜单是;
导航图;
<?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/client_nav_graph"
app:startDestination="@id/clientDetailsFragment">
<fragment
android:id="@+id/clientCustomersFragment"
android:name="com.management.engineering.alarm.alarmengineermanagement.features.client.ClientCustomersFragment"
android:label="ClientCustomersFragment"
tools:layout="@layout/fragment_client_customers" />
<fragment
android:id="@+id/clientDetailsFragment"
android:name="com.management.engineering.alarm.alarmengineermanagement.features.client.ClientDetailsFragment"
android:label="ClientDetailsFragment"
tools:layout="@layout/fragment_client_details"/>
<fragment
android:id="@+id/clientJobHistoryFragment"
android:name="com.management.engineering.alarm.alarmengineermanagement.features.client.ClientJobHistoryFragment"
android:label="ClientJobHistoryFragment"
tools:layout="@layout/fragment_client_job_history" />
</navigation>
菜单;
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/clientDetailsFragment"
android:icon="@drawable/ic_launcher_foreground"
android:title="Details"/>
<item
android:id="@+id/clientJobHistoryFragment"
android:icon="@drawable/ic_launcher_foreground"
android:title="Job History"/>
<item
android:id="@+id/clientCustomersFragment"
android:icon="@drawable/ic_launcher_foreground"
android:title="Customers"/>
</menu>
我发现您可以向导航图添加参数,但是对于这种特定情况,并没有找到将它们放置在何处的信息,我也知道能够在使用.navigate进行导航时手动添加包。
我是否可以在ClientFragment中为每个片段设置参数
viewModel.client
更新:
我的自变量问题是通过使用在BottomNavigationView中的所有片段之间共享的视图模型解决的(我在向朋友键入问题时意识到了这一点),并且导航本身将其添加到ClientFragment; / p>
bottom_nav_client.setupWithNavController(
Navigation.findNavController(
view.findViewById<View>(R.id.fl_client_nav_container)
)
)
和我用于fragment_client的xml;
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_client"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="?attr/NavigationBackIconLight"
app:titleTextColor="@color/white" />
<fragment
android:id="@+id/fl_client_nav_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/bottom_nav_client"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar_client"
app:navGraph="@navigation/client_nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav_client"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="?android:attr/windowBackground"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@drawable/bottom_nav_color"
app:itemTextColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/client_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
与上面显示的相同导航图和菜单结合在一起。
答案 0 :(得分:3)
接受的答案中提到的代码实验室没有提及将参数传递给BottomNavigationView中的片段。
使用自定义参数覆盖由setupWithNavController()设置的OnNavigationItemSelectedListener:
val args = Bundle()
bottomNavigationView.setupWithNavController(navController)
bottomNavigationView.setOnNavigationItemSelectedListener { item ->
navController.navigate(item.itemId, args)
true
}
答案 1 :(得分:0)
导航体系结构组件 documentation显示了如何定义目标参数,在具体情况下,您应该创建一个自定义Parcelable
类(即Client
),并将其作为相应片段的参数。
<fragment
android:id="@+id/clientCustomersFragment"
android:name="com.management.engineering.alarm.alarmengineermanagement.features.client.ClientCustomersFragment"
android:label="ClientCustomersFragment"
tools:layout="@layout/fragment_client_customers" >
<action
android:id="@+id/client_to_details"
app:destination="@+id/clientDetailsFragment" />
</fragment>
<fragment
android:id="@+id/clientDetailsFragment"
android:name="com.management.engineering.alarm.alarmengineermanagement.features.client.ClientDetailsFragment"
android:label="ClientDetailsFragment"
tools:layout="@layout/fragment_client_details">
<argument
android:name="client"
app:argType="com.management.engineering.alarm.alarmengineermanagement.features.client.Client" />
</fragment>
'androidx.navigation.safeargs'
gradle插件将生成可用于传递的类ClientToDetails
和ClientDetailsFragmentArgs
。检索参数client
。
来源
val client: Client = TODO()
val navController: NavController = TODO()
navController.navigate(ClientToDetails(client))
目的地
val client = ClientDetailsFragmentArgs.fromBundle(arguments).client
答案 2 :(得分:0)