我无法在 viewpager2 中对 TextView 使用数据绑定,postValue() 无效
I cannot use databinding with TextView in viewpager2, postValue() is invalid
我在使用数据绑定和viewpager2 时遇到问题。我在片段中使用了 viewpager2。 viewpager2 中的 FragmentA 想要共享片段的视图模型。
- 在fragmentA中绑定了一个TextView属性文本,但是viewmodel中的postValue无法改变TextView的文本
- 但是fragmentA绑定了点击事件(TextView有点击,触发sendCode()函数),可以在viewmodel中触发
片段A:
class SignUpMainFragment(val vm:SignUpFragmentVM):Fragment() {
private var mBinding:FragmentSignUpMainBinding?=null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_sign_up_main,container,true)
mBinding?.signUp = vm
return mBinding?.root!!
}
}
fragmentA布局:
<?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="signUp"
type="com.xxx.SignUpFragmentVM" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/et_email"
android:layout_width="468dp"
android:layout_height="94dp"
android:layout_marginStart="40dp"
android:hint="Email"
android:text="@={signUp.txtEmail}"
android:textSize="30sp"
android:textColor="@color/color_aaa280"
android:drawableStart="@mipmap/img_mail"
android:paddingStart="26dp"
android:drawablePadding="18dp"
android:inputType="textEmailAddress"
android:background="@drawable/shape_edit_bg_e7e7e7"
app:layout_constraintTop_toTopOf="@id/et_name"
app:layout_constraintStart_toEndOf="@id/et_name" />
<TextView
android:id="@+id/tv_send_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{signUp.txtSendEmail}"
android:textSize="30.5sp"
android:textColor="@color/color_71cbc0"
android:onClick="@{signUp.click}"
android:clickable="true"
android:layout_marginStart="25dp"
app:layout_constraintTop_toTopOf="@id/et_lock_psw"
app:layout_constraintBottom_toBottomOf="@id/et_lock_psw"
app:layout_constraintStart_toEndOf="@id/et_lock_psw"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
SignUpFragmentVM:
class SignUpFragmentVM constructor():ViewModel() {
val txtSendEmail = MutableLiveData("Get verified.")
....
private fun sendCode(){
viewModelScope.launch {
...
//TODO:60s Countdown
txtSendEmail.postValue("60")
}
}
}
你需要更多
mBinding?.signUp = vm
mBinding?.lifecycleOwner = this // => add
我在使用数据绑定和viewpager2 时遇到问题。我在片段中使用了 viewpager2。 viewpager2 中的 FragmentA 想要共享片段的视图模型。
- 在fragmentA中绑定了一个TextView属性文本,但是viewmodel中的postValue无法改变TextView的文本
- 但是fragmentA绑定了点击事件(TextView有点击,触发sendCode()函数),可以在viewmodel中触发
片段A:
class SignUpMainFragment(val vm:SignUpFragmentVM):Fragment() {
private var mBinding:FragmentSignUpMainBinding?=null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_sign_up_main,container,true)
mBinding?.signUp = vm
return mBinding?.root!!
}
}
fragmentA布局:
<?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="signUp"
type="com.xxx.SignUpFragmentVM" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/et_email"
android:layout_width="468dp"
android:layout_height="94dp"
android:layout_marginStart="40dp"
android:hint="Email"
android:text="@={signUp.txtEmail}"
android:textSize="30sp"
android:textColor="@color/color_aaa280"
android:drawableStart="@mipmap/img_mail"
android:paddingStart="26dp"
android:drawablePadding="18dp"
android:inputType="textEmailAddress"
android:background="@drawable/shape_edit_bg_e7e7e7"
app:layout_constraintTop_toTopOf="@id/et_name"
app:layout_constraintStart_toEndOf="@id/et_name" />
<TextView
android:id="@+id/tv_send_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{signUp.txtSendEmail}"
android:textSize="30.5sp"
android:textColor="@color/color_71cbc0"
android:onClick="@{signUp.click}"
android:clickable="true"
android:layout_marginStart="25dp"
app:layout_constraintTop_toTopOf="@id/et_lock_psw"
app:layout_constraintBottom_toBottomOf="@id/et_lock_psw"
app:layout_constraintStart_toEndOf="@id/et_lock_psw"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
SignUpFragmentVM:
class SignUpFragmentVM constructor():ViewModel() {
val txtSendEmail = MutableLiveData("Get verified.")
....
private fun sendCode(){
viewModelScope.launch {
...
//TODO:60s Countdown
txtSendEmail.postValue("60")
}
}
}
你需要更多
mBinding?.signUp = vm
mBinding?.lifecycleOwner = this // => add