Android导航组件-接收一个参数的外部包含图形

bakd9h0s  于 2023-01-19  发布在  Android
关注(0)|答案(2)|浏览(122)

我正在使用Jetpack的导航组件。
我的主图非常大,因此我决定将其拆分为不同的图,并使用<include />功能。
问题是,其中一些图表接收参数,但Android在创建导航Directions时似乎忽略了这些参数
即:
主图(简化)

<?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">
    <fragment
        android:id="@+id/listFragment"
         android:name="com......ListFragment">
        <action
            android:id="@+id/view_detail"
            app:destination="@id/item_detail"/>
    </fragment>

    <include app:graph="@navigation/included_graph"/>

</navigation>

包含的图表:(简化)

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_detail"
    app:startDestination="@id/itemDetailFragment">
    <argument
        android:name="item_id"
        app:argType="integer" />
    <fragment
        android:id="@+id/itemDetailFragment"
        android:name="com......ItemDetailFragment">

        <argument
            android:name="item_id"
            app:argType="integer" />
    </fragment>
    [...]
</navigation>

android生成的类为

class ListFragmentDirections private constructor() {
    companion object {
        fun viewDetail(): NavDirections =
                ActionOnlyNavDirections(R.id.view_detail)
    }
}

被包含的图接收到的参数似乎被忽略了--我无法安全地传递任何参数。
是否有解决方案?
我对任何能让我将图形分解成更小文件的替代方案都很感兴趣,不管是否基于<include />

mspsb9vt

mspsb9vt1#

有一个解决方案:包括动作的参数,即

<?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">
    <fragment
        android:id="@+id/listFragment"
         android:name="com......ListFragment">
        <action
            android:id="@+id/view_detail"
            app:destination="@id/item_detail">
            <argument
                android:name="item_id"
                app:argType="integer" />
        </action>
    </fragment>

    <include app:graph="@navigation/included_graph"/>

</navigation>
bxpogfeg

bxpogfeg2#

要解决此问题,您需要执行以下操作:
1.在Included Graph中,从<navigation>块中删除item_id<argument>
1.在<navigation>块的Included Graph中,将目的地添加到@id/itemDetailDragment

<action
    android:id="@+id/view_detail"
    app:destination="@id/itemDetailDragment"/>

1.在Main Graph中,确保所需的<action> ID与Included Graph<action>的ID相同,在您的情况下,它应该保持不变。

  • 此外,要验证目的地(如下所示),可以从Main Graph中的<action> id中删除+符号,因为在这种情况下,它应该使用Included Graph中的<action>的id。*

总的来说,你的代码应该是这样的:

    • 主图表**
<?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">

    <fragment
        android:id="@+id/listFragment"
        android:name="com......ListFragment">

        <action
            android:id="@id/view_detail"
            app:destination="@id/item_detail"/>
    </fragment>

    <include app:graph="@navigation/included_graph"/>

</navigation>
    • 包含的图表**
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_detail"
    app:startDestination="@id/itemDetailFragment">  

    <action
        android:id="@+id/view_detail"
        app:destination="@id/itemDetailDragment"/>
    
    <fragment
        android:id="@+id/itemDetailFragment"
        android:name="com......ItemDetailFragment">

        <argument
            android:name="item_id"
            app:argType="integer" />
    </fragment>
</navigation>

相关问题