simple-range-view icon indicating copy to clipboard operation
simple-range-view copied to clipboard

Two-way Layout Databinding for start and end

Open mcanyucel opened this issue 5 years ago • 0 comments

Instead of using the callback functions to get the new values, I tried to data-bind them to my view model. When the data-binding is one way, the control can get the value from the view-model without any issues:

<?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"
    tools:context=".MainActivity">
    <data>
        <variable
            name="mainViewModel"
            type="com.bridgewiz.laserdotdetection.viewmodel.MainViewModel" />
    </data>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <me.bendik.simplerangeview.SimpleRangeView
            android:id="@+id/fixed_rangeview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|top"
            app:count="180"
            app:start="@{mainViewModel.hueMin}"
            app:end="@{mainViewModel.hueMax}"
            app:minDistance="20" />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

Viewmodel:

...
    val hueMin= MutableLiveData(5)
    val hueMax= MutableLiveData(160)
...

Of course in this case the changes by user are not reflected to viewmodel. Therefore when if I change the binding to two-way then the compilation fails:

            app:start="@={mainViewModel.hueMin}"
Cannot find a getter for <me.bendik.simplerangeview.SimpleRangeView app:start> that accepts parameter type 'java.lang.Integer'
If a binding adapter provides the getter, check that the adapter is annotated correctly and that the parameter type matches.

Do I need a custom adapter for this? If I need one, I don't get how to write the adapter because it seems to me like the type Int is correct and there is nothing to adapt for the adapter.

mcanyucel avatar Mar 10 '21 14:03 mcanyucel