Home > Software engineering >  viewpager2 data cannot be bound using databinding
viewpager2 data cannot be bound using databinding

Time:11-20

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>

CodePudding user response:

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

  • Related