Android - DialogFragment 不显示我放入其中的 ImageView

Android - DialogFragment doesn't show the ImageView that I put into it

我遇到了一个实际上不应该发生的错误,因为我在我的项目中在另一个屏幕上做了完全相同的事情,并且它在那里工作,但不想在另一个屏幕上工作。

问题如下:由于某些操作,我从 Activity 打开了一个 DialogFragment,它在其布局文件中包含图像和其他视图。现在我不明白为什么,但它在第一种情况下有效(您将在下面看到)但在第二种情况下不起作用...

第一种情况:

布局XML文件(dialog_character_selector.xml):

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="dialogViewModel"
            type="neptun.jxy1vz.cluedo.ui.menu.character_selector.CharacterSelectorViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Spinner
            android:id="@+id/spinnerCharacter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/ivCharacterCard"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="20dp"
            android:src="@drawable/szereplo_hatlap"
            app:layout_constraintBottom_toTopOf="@+id/btnStart"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_percent="0.7"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/spinnerCharacter" />

        <Button
            android:id="@+id/btnStart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/start"
            android:layout_marginBottom="10dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:onClick="@{()->dialogViewModel.startGame()}"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

DialogFragment的Kotlin源码(CharacterSelectorDialog.kt):

package neptun.jxy1vz.cluedo.ui.menu.character_selector

import android.animation.AnimatorInflater
import android.animation.AnimatorSet
import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.appcompat.app.AlertDialog
import androidx.core.animation.doOnEnd
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import neptun.jxy1vz.cluedo.R
import neptun.jxy1vz.cluedo.databinding.DialogCharacterSelectorBinding

class CharacterSelectorDialog : DialogFragment(), AdapterView.OnItemSelectedListener {

    private lateinit var dialogCharacterSelectorBinding: DialogCharacterSelectorBinding

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        dialogCharacterSelectorBinding = DataBindingUtil.inflate(
            LayoutInflater.from(context),
            R.layout.dialog_character_selector,
            null,
            false
        )

        dialogCharacterSelectorBinding.spinnerCharacter.adapter = ArrayAdapter<String>(
            context!!,
            android.R.layout.simple_spinner_dropdown_item,
            resources.getStringArray(R.array.characters)
        )
        dialogCharacterSelectorBinding.spinnerCharacter.onItemSelectedListener = this

        //I do this due to a card flipping animation, it's not important, not part of my problem
        val scale = resources.displayMetrics.density
        dialogCharacterSelectorBinding.ivCharacterCard.cameraDistance = 8000 * scale

        dialogCharacterSelectorBinding.dialogViewModel = CharacterSelectorViewModel(context!!)

        return AlertDialog.Builder(context!!, R.style.Theme_AppCompat_Light_Dialog).setView(dialogCharacterSelectorBinding.root).setTitle(resources.getString(R.string.dialog_character_title)).create()
    }

    override fun onNothingSelected(parent: AdapterView<*>?) {
        dialogCharacterSelectorBinding.ivCharacterCard.setImageResource(R.drawable.szereplo_hatlap)
    }

    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        dialogCharacterSelectorBinding.ivCharacterCard.setImageResource(R.drawable.szereplo_hatlap)
        (AnimatorInflater.loadAnimator(context, R.animator.card_flip) as AnimatorSet).apply {
            setTarget(dialogCharacterSelectorBinding.ivCharacterCard)
            start()
            doOnEnd {
                dialogCharacterSelectorBinding.dialogViewModel!!.setPlayer(position)

                val img = when (position) {
                    0 -> R.drawable.szereplo_ginny
                    1 -> R.drawable.szereplo_harry
                    2 -> R.drawable.szereplo_hermione
                    3 -> R.drawable.szereplo_ron
                    4 -> R.drawable.szereplo_luna
                    else -> R.drawable.szereplo_neville
                }
                dialogCharacterSelectorBinding.ivCharacterCard.setImageResource(img)
            }
        }
    }
}

第二种情况:

布局XML文件(dialog_helper_card.xml):

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <variable
            name="helperCardDialogViewModel"
            type="neptun.jxy1vz.cluedo.ui.dice.card_dialog.helper.HelperCardViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/ivHelperCard"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="20dp"
            android:src="@drawable/mento_hatlap"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

我觉得主要部分和上一个一样

Kotlin 源文件(HelperCardDialog.kt):

package neptun.jxy1vz.cluedo.ui.dice.card_dialog.helper

import android.animation.AnimatorInflater
import android.animation.AnimatorSet
import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AlertDialog
import androidx.core.animation.doOnEnd
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import neptun.jxy1vz.cluedo.R
import neptun.jxy1vz.cluedo.databinding.DialogHelperCardBinding

class HelperCardDialog(private val cardResource: Int) : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): AlertDialog {
        val dialogHelperCardBinding = DataBindingUtil.inflate<DialogHelperCardBinding>(LayoutInflater.from(context), R.layout.dialog_helper_card, null, false)
        dialogHelperCardBinding.helperCardDialogViewModel = HelperCardViewModel()

        (AnimatorInflater.loadAnimator(context, R.animator.card_flip) as AnimatorSet).apply {
            setTarget(dialogHelperCardBinding.ivHelperCard)
            start()
            doOnEnd {
                dialogHelperCardBinding.ivHelperCard.setImageResource(cardResource)
            }
        }

        return AlertDialog.Builder(context!!, R.style.Theme_AppCompat_Dialog).setTitle(resources.getString(R.string.got_helper_card)).setNeutralButton(resources.getString(R.string.ok)
        ) { dialog, _ ->
            dialog.dismiss()
        }.create()
    }
}

就是这样。这些是我遇到的问题中最重要的文件。抱歉代码太多...

我希望你能看到问题出在哪里,并告诉我解决方法是什么。

终于我在我的代码中找到了错误。我离开了第二个 AlertDialog.Builder().

的 setView() 函数调用

正确的代码片段是:

return AlertDialog.Builder(context!!, R.style.Theme_AppCompat_Dialog)
            .setView(dialogHelperCardBinding.root)
            .setTitle(resources.getString(R.string.got_helper_card)).setNeutralButton(
            resources.getString(R.string.ok)
        ) { dialog, _ ->
            dialog.dismiss()
        }.create()