我目前正在编写一个带有新的(对我来说)导航组件的应用程序.我有一个导航图来导航我的应用程序,我有一个带有BottomNavigationView的片段,其中有3个独立的片段,我已经设法更新了它,使用导航组件(就我的问题而言),使用与导航项匹配的ID菜单.我的片段以前都使用newInstance方法将bundle传递给onCreate,现在显然没有使用,但我仍然需要将bundle传递给我的片段.

我找不到任何这样做的示例,因为片段是隐式创建的.

我的代码 struct 为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() }
    }
}

This class previously held on onclick listener to my fragments, with a newInstance method which tool viewModel.client.

My fragments in the nav_graph are all similar. The first fragment;

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)!! 

My navigation graph and menu are;

nav graph;

<?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>

我发现你可以在导航图中添加参数,但是对于这个特定的场景,你找不到将参数放在哪里的方法,我也知道在使用导航时可以手动添加Bundle 包.导航

Is there a way for me to set in my ClientFragment the arguments for each of these fragments to be

viewModel.client

Update:

我的争论问题通过使用在BottomNavigationView中的所有片段之间共享的视图模型(我在向朋友键入问题时意识到了这一点)和导航本身(我将此添加到ClientFragment中)来解决;

bottom_nav_client.setupWithNavController(
            Navigation.findNavController(
                    view.findViewById<View>(R.id.fl_client_nav_container)
            )
    )

我的xml用于fragment_客户端;

<?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>

This is combined with the same navigation graph and menu as shown above.

Kotlin相关问答推荐

Kotlin—从列表中枚举属性计算不同值的数量

查看流数据和改进的HTTP请求的模型

如果启用了Flyway迁移,则不能具有配置属性';datources.default.架构-生成

如何定义一个函数来接受任何具有特定字段的数据类

如何更改默认推断没有接收者的函数类型?

创建包含 3 个相同项目的列表/使用返回类型重复

jlink:在合并模块和 kotlin.stdlib 中打包 kotlin.*

Kotlin 函数有 2 个参数用于对 Map 或 List 进行排序

为什么我们在 kotlin 中需要 noinline?

JobIntentService 被销毁,当应用程序被销毁时

如何在 Kotlin 中为变量设置监听器

在 Koin 中提供一个 Instance 作为其接口

如何使用Kotlin Dokka记录主构造函数参数

Kotlin内联扩展属性

Kotlin 中的内联构造函数是什么?

如何解决:将Java类转换为Kotlin后出现error: cannot find symbol class ...?

如何暂停kotlin coroutine直到收到通知

类型不匹配推断类型为单位,但应为空

将字符串转换为HashMap的最简单方法

目前不支持 Gradle 项目的自动库版本更新.请手动更新您的 build.gradle