【问题标题】:How to passing image view from fragment loaded by retrofit to childfragment?如何将图像视图从改造加载的片段传递给子片段?
【发布时间】:2021-05-16 18:06:05
【问题描述】:

所以我有 1 个主片段和 2 个子片段,我想将图像视图从主片段传递到子片段,而无需在该 2 个子片段中再次获取请求改造怎么做??

我已经在我的主要 Fargment 中尝试过这个:

private lateinit var imgFile: File

......

intent.putExtra("resId", imgFile)

然后在子片段中调用它:

val bundle: Bundle = intent.extras
    val resId: Int = bundle.getInt("resId")
    binding.imgmainac.setImageResource(resId)

【问题讨论】:

    标签: android kotlin android-fragments fragment pass-data


    【解决方案1】:

    您总是可以直接从子片段中直接访问您的父片段字段

    这样试试

    (parentFragment as MainFragment).imgFile
    

    【讨论】:

    • 您能告诉我更多代码吗?先生?
    • 这一行就足够了。无需编写更多代码
    【解决方案2】:

    您在这里混淆了两件事:

    • resId 只是一个标识内部应用资源的 Int。
    • 一个文件

    文件不是 resId 或 Int。

    所以如果你已经有一个文件,我会建议:

    • 将文件 URI 作为字符串放入包中(可以使用 file.toURI().toString() 获取)
    • 使用 Uri.parse(bundle.getString(...)) 检索它
    • 使用 .setImageURI(...) 在 ImageView 上设置它

    编辑:

    总结一下:

    在发送者端

        intent.putExtra("imageUri", file.toURI().toString())
    

    在接收端

        val bundle = intent.extras
        val imageUri = bundle?.getString("imageUri")?.let { Uri.parse(it) }
        imageView.setImageURI(imageUri)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多