kotlin - 使用导航组件在 BottomNavigationView 中传递参数

标签 kotlin androidx

我目前正在使用新的(对我而言)导航组件编写应用程序。我已经通过一个导航图来了解我的应用程序的基础知识,我有一个带有 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() }
    }
}

这个类以前持有 onclick 监听器到我的片段,使用工具 viewModel.client 的 newInstance 方法。

我在 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 中的所有片段之间共享的 View 模型解决的(我在向 friend 输入问题时意识到这一点)和导航本身,我将其添加到 ClientFragment;
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>

这与如上所示的相同导航图和菜单相结合。

最佳答案

关于kotlin - 使用导航组件在 BottomNavigationView 中传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53974342/

相关文章:

android - MutableLiveData<Pair<Float, Float>>() -> Val 不能被重新赋值

android - 谷歌身份验证迁移到 androidx : SignInHubActivity does not implement Lifecycle

Kotlin java String.format 数组值设置

android - 从 Activity 中获取嵌套在导航 Controller 内的 fragment 实例

android - ClassCastException ContentFrameLayout androidx 与系统应用程序中的 support.v7

android - Fragment NavHostFragment 没有创建 View

android - 如何在 AndroidX 中更改 Android SearchView 的 textcolor 或 queryhint textcolor?

json - Kotlin:来自Json的GSON无法计算

android - 无法将 Kotlin Android 扩展添加到我的项目中

java - 奇怪的是找不到符号 '@IntDef' 但 Android Studio 没有显示错误