android - 具有 View 模型的 DialogFragment 无法使用数据绑定(bind)

标签 android kotlin mvvm android-databinding android-livedata

我用 View 模型(mvvm)创建了一个对话框 fragment 。对话框由一个按钮(自定义 View )组成。当使用具有数据绑定(bind)的 View 模型时,当 livedata 更改时按钮单击不起作用。我使用 bool 值来检查按钮是否被单击。是什么导致问题?如果需要,还建议任何其他方法。

profile_dialog_fragment.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"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="viewmodel"
            type="com.test.ui.ProfileDialogViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.ProfileDialog">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/login"
            style="@style/TextAppearance.MaterialComponents.Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Login"
            android:onClick="@{() -> viewmodel.onLoginButtonClick()}"
            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>

ProfileDialog.kt
class ProfileDialog : DialogFragment() {

    companion object {
        fun newInstance() = ProfileDialog()
    }

    private val viewModel: ProfileDialogViewModel by viewModel()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val binding = ProfileDialogFragmentBinding.inflate(inflater, container, false)
            .apply {
                this.lifecycleOwner = this@ProfileDialog
                this.viewmodel = viewmodel
            }
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        viewModel.startLogin.observe(viewLifecycleOwner, Observer {

            Log.d("insta", "This is working")
            if (it == null) return@Observer

            if(it) {
                Log.d("insta", "This is not working")
                val loginIntent = Intent(this.context, LoginActivity::class.java)
                this.context?.startActivity(loginIntent)
            }
        })
    }

}

ProfileDialogViewModel.kt
class ProfileDialogViewModel : ViewModel() {

    private val _startLogin = MutableLiveData<Boolean>(false)

    val startLogin: LiveData<Boolean>
        get() = _startLogin

    fun onLoginButtonClick() {
        Log.d("insta", "This ain't working")
        _startLogin.postValue(true)
    }
}

最佳答案

您的 View 模型定义在

private val viewModel: ProfileDialogViewModel by viewModel()

所以,注意查看 M odel。问题位于
this.viewmodel = viewmodel

其中 this 指向 ProfileDialogFragmentBinding 。在这里,您正在分配 ProfileDialogFragmentBinding.viewmodel = ProfileDialogFragmentBinding.viewmodel - 这就是它不起作用的原因。

要解决问题,请像这样正确分配它:
this.viewmodel = viewModel

关于android - 具有 View 模型的 DialogFragment 无法使用数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61757504/

相关文章:

ios - 在 MVVM 模式中使用闭包将 View 模型与 View Controller 绑定(bind)是一种好的做法吗?

wpf - 如何在 MVVM 中实现 INotifyCollectionChanged 以重置、添加、删除、移动、替换 NotifyCollectionChangedAction

android - 如何更新子集合Firestore?

android - 房间 (AAC) : [SQLITE_ERROR] SQL error or missing database (near "group": syntax error)

当新数据插入listview时Android自动刷新

retrofit - Kotlin 特性和改造

android - kotlin 'onCreate' 不会覆盖任何内容

c# - WPF 中的 MVVM - 如何提醒 ViewModel 模型中的变化......或者我应该吗?

Android 陀螺仪 - OpenGL LookAt 位置

java - 如何通过 Java 更改 strings.xml 中的值 (Android)