从 firebase 检索 gif 图像和 returns 相同的 gif 图像,如何解决这个问题?

Retrieve gif image from firebase and returns the same gif image, how to fix this?

我的应用程序有问题, 我目前正在使用 ViewPager 构建一个带有水平滑块的信息页面。我有 2 个布局 XML 与 1 个 activity 和 ViewPager 结合在一起,这意味着每个布局显示不同的 gif。所有 gif 图像都是从 firebase 检索的。问题是所有布局显示相同的gif。

这是我的 onCreate 函数:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_practice)

    val gifImage1 = FirebaseStorage.getInstance().reference.child("gif/step1.gif")
    val gifImage2 = FirebaseStorage.getInstance().reference.child("gif/step2_left.gif")
    val localFile = File.createTempFile("tempGif", "gif")

    gifImage1.getFile(localFile).addOnSuccessListener {
        val gifFromPath = GifDrawable(localFile.absolutePath)
        img.setImageDrawable(gifFromPath)
    }.addOnFailureListener {
        Toast.makeText(this, "Failed to retrieve the image", Toast.LENGTH_SHORT).show()
    }

    gifImage2.getFile(localFile).addOnSuccessListener {
        val gif2FromPath = GifDrawable(localFile.absolutePath)
        img2.setImageDrawable(gif2FromPath)
    }.addOnFailureListener {
        Toast.makeText(this, "Failed to retrieve the image", Toast.LENGTH_SHORT).show()
    }
    
    init()
    dataSet()
    interaction()
}

在我的示例布局下方 XML:

第一个布局的屏幕截图 XML 下面:

第二个布局的屏幕截图 XML 下面:

上面的两个布局应该有不同的gif图像。 任何帮助和解决方案,我非常感谢。

您对两张图片使用了相同的localFile

val localFile = File.createTempFile("tempGif", "gif")

您应该将它们下载到单独的文件中,而不是使用同一个文件。