【问题标题】:Default argument value for action in Jetpack NavigationJetpack Navigation 中操作的默认参数值
【发布时间】:2020-07-23 14:19:48
【问题描述】:

是否可以使用 Jetpack Navigation 为导航操作设置默认参数值?

我有一个需要参数的fragment。当使用navController.navigate(R.id.action1)"2" 时,我希望这个参数的值是"1" navController.navigate(R.id.action2)

<fragment
    android:id="@+id/myFragment"
    android:name="MyFragment">

    <argument android:name="action_number" />

</fragment>

<!-- action_number should be set to 1 -->
<action
    android:id="@+id/action1"
    app:destination="@id/myFragment" />

<!-- action_number should be set to 2 -->
<action
    android:id="@+id/action2"
    app:destination="@id/myFragment" />

【问题讨论】:

    标签: android android-architecture-navigation android-jetpack-navigation


    【解决方案1】:

    使用 Safe Args 以安全类型传递数据

    要将 Safe Args 添加到您的项目中,请在顶级 build.gradle 文件中包含以下类路径:

    buildscript {
        repositories {
            google()
        }
        dependencies {
            def nav_version = "2.3.0"
            classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
        }
    }
    

    要生成适用于 Java 或混合 Java 和 Kotlin 模块的 Java 语言代码,请将此行添加到您的应用或模块的 build.gradle 文件中:

    apply plugin: "androidx.navigation.safeargs"
    

    传递参数Kotlin

    override fun onClick(v: View) {
       val amountTv: EditText = view!!.findViewById(R.id.editTextAmount)
       val amount = amountTv.text.toString().toInt()
       val action = SpecifyAmountFragmentDirections.confirmationAction(amount)
       v.findNavController().navigate(action)
    }
    

    Java:

    @Override
    public void onClick(View view) {
       EditText amountTv = (EditText) getView().findViewById(R.id.editTextAmount);
       int amount = Integer.parseInt(amountTv.getText().toString());
       ConfirmationAction action =
               SpecifyAmountFragmentDirections.confirmationAction()
       action.setAmount(amount)
       Navigation.findNavController(view).navigate(action);
    }
    

    在您接收目的地的代码中,使用getArguments() 方法检索捆绑包并使用其内容

    科特林:

    val args: ConfirmationFragmentArgs by navArgs()
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val tv: TextView = view.findViewById(R.id.textViewAmount)
        val amount = args.amount
        tv.text = amount.toString()
    }
    

    Java:

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        TextView tv = view.findViewById(R.id.textViewAmount);
        int amount = ConfirmationFragmentArgs.fromBundle(getArguments()).getAmount();
        tv.setText(amount + "")
    }
    

    更多详情:Pass data between destinations

    【讨论】:

    • 这样我必须在调用navigate之前传递参数的值(比如在SpecifyAmountFragmentDirections.confirmationAction(amount)中传递amount)。我正在寻找一个默认设置参数值的解决方案 - 每个 &lt;action&gt; 具有不同的值
    【解决方案2】:

    您可以使用&lt;argument&gt; 标记在&lt;action&gt; 标记内提供默认值。

    例如,您的第一个操作将变为:

        <action
            android:id="@+id/action1"
            app:destination="@id/myFragment">
            <argument
                android:name="action_number"
                android:defaultValue="1"/>
        </action>
    

    【讨论】:

      【解决方案3】:

      在参数中使用默认值,

      例如:-

      <action
          android:id="@+id/action1"
          app:destination="@id/myFragment">
          <argument
              android:name="value"
              android:defaultValue="1"/>
      </action>ode here
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-01
        • 2013-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多