具有视图模型的DialogFragment无法与数据绑定一起使用

问题描述 投票:0回答:1

我已经创建了一个带有视图模型(mvvm)的对话框。对话框包含一个按钮(自定义视图)。当使用具有数据绑定的视图模型时,实时数据更改时按钮单击不起作用。我使用布尔值检查按钮是否被单击。是什么原因引起的?如果需要,还建议任何其他方法。

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)
    }
}
android kotlin mvvm android-databinding android-livedata
1个回答
0
投票

您的视图模型在]中定义>

private val viewModel: ProfileDialogViewModel by viewModel()

所以,请注意查看M

odel。问题位于
this.viewmodel = viewmodel

其中this指向ProfileDialogFragmentBinding。在这里您要输入ProfileDialogFragmentBinding.viewmodel = ProfileDialogFragmentBinding.viewmodel-这就是为什么它不起作用的原因。

要解决问题,请像这样正确分配它:

this.viewmodel = viewModel
© www.soinside.com 2019 - 2024. All rights reserved.