0

I encountered a problem when using databinding and viewpager2. I used viewpager2 in a fragment. FragmentA in viewpager2 wanted to share the viewmodel of the fragment.

  1. An EditText attribute text is bound in fragmentA, but the value of EditText cannot be obtained in the viewmodel.
  2. But fragmentA is bound to a click event, which can be triggered in the viewmodel

I don’t know where I am doing it wrong, the first point will be invalid

fragmentA:

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!!
    }
}

Layout of 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>
GaleLiu
  • 49
  • 4

1 Answers1

0

If your goal to get data in viewModel when user enter some text inside your editTextView then you should use two way data binding.

Here is example:

<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}" //<---- two way dataBinding
            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" />

The @={} notation, which importantly includes the "=" sign, receives data changes to the property and listen to user updates at the same time.

more here two way dataBinding

chand mohd
  • 2,363
  • 1
  • 14
  • 27
  • 1
    Wow, this is amazing, Thank you very much, this solved my problem. But when I didn't use viewpager2 (in normal layout), I didn't use two-way binding. Viewmodel can get the text of edittext, which makes me feel puzzled. – GaleLiu Nov 20 '21 at 07:47
  • Next I encountered another problem. Another control in FragmentA is the button (TextView) that sends the verification code. When I press it, it needs to have a 60s countdown, so I use postValue to change the @{signUp.txtSendEmail} of the TextView. But it doesn't work, do you know the problem? – GaleLiu Nov 20 '21 at 08:16
  • @chandmohd can you check this [issue](https://stackoverflow.com/a/69813808/11560810) – Kotlin Learner Jan 11 '22 at 17:14