Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin 2-way binding custom view

I have 1 custom view that extends ConstraintLayout and contains 1 EditText and 2 TextViews

On my custom view i define this attr (and others) :

<attr name="Text" format="string" />

and i use it like :

app:Text="@={login.email}"

Inside my custom view i define :

  companion object {
    @JvmStatic @BindingAdapter("Text")
    fun setText(nMe : View, nText: String) {
        nMe.nInput.setText(nText)
    }
    @InverseBindingAdapter(attribute = "Text")
    fun getText(nMe : View) : String {
      return  nMe.nInput.text.toString()
    }

witch works fine in one-way binding

app:Text="@{login.email}"

But when i try to use it in 2-way binding i get erros pointing to ActivityLoginBinding.java java.lang.String callbackArg_0 = mBindingComponent.null.getText(mEmail);

What to do to get 2-way binding?

L.E : After some research i end up with this :

@InverseBindingMethods(InverseBindingMethod(type = 
CustomInput::class,attribute = "bind:Text",event = 
"bind:textAttrChanged",method = "bind:getText"))
class CustomEditTextBinder {
companion object {
    @JvmStatic
    @BindingAdapter(value = ["textAttrChanged"])
    fun setListener(editText: CustomInput, listener: InverseBindingListener?) {
        if (listener != null) {
            editText.nInput.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {

                }

                override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {

                }

                override fun afterTextChanged(editable: Editable) {
                    listener.onChange()
                }
            })
        }
    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "Text")
    fun getText(nMe: CustomInput): String {
        return nMe.nInput.text.toString()
    }

    @JvmStatic
    @BindingAdapter("Text")
    fun setText(editText: CustomInput, text: String?) {
        text?.let {
            if (it != editText.nInput.text.toString()) {
                editText.nInput.setText(it)
            }
        }
    }
}

}

But right now i get : Could not find event TextAttrChanged

like image 900
BejanCorneliu Avatar asked Aug 28 '18 17:08

BejanCorneliu


People also ask

Can I use both data binding and view binding?

View binding doesn't support layout variables or layout expressions, so it can't be used to declare dynamic UI content straight from XML layout files. View binding doesn't support two-way data binding.

What is two-way binding in Kotlin?

Stay organized with collections Save and categorize content based on your preferences. The @={} notation, which importantly includes the "=" sign, receives data changes to the property and listen to user updates at the same time. // Avoids infinite loops.

Which is better view binding or data binding?

ViewBinding vs DataBindingThe main advantages of viewbinding are speed and efficiency. It has a shorter build time because it avoids the overhead and performance issues associated with DataBinding due to annotation processors affecting DataBinding's build time.

What is the difference between one-way data binding and two-way data binding Android?

In one-way binding, the flow is one-directional. In a two-way binding, the flow is two-directional. This means that the flow of code is from ts file to Html file. This means that the flow of code is from ts file to Html file as well as from Html file to ts file.


1 Answers

I think all you need is event = "android:textAttrChanged".

This works for me (set text to empty String if it is 0):

object DataBindingUtil {
    @BindingAdapter("emptyIfZeroText")
    @JvmStatic
    fun setText(editText: EditText, text: String?) {
        if (text == "0" || text == "0.0") editText.setText("") else editText.setText(text)
    }

    @InverseBindingAdapter(attribute = "emptyIfZeroText", event = "android:textAttrChanged")
    @JvmStatic
    fun getText(editText: EditText) = editText.text.toString()
}
like image 176
Sam Chen Avatar answered Oct 18 '22 18:10

Sam Chen