Skip to content

chetdeva/spinner-bindings

Repository files navigation

Spinner Bindings

SpinnerBindings expounds on how we can bind a Spinner with DataBinding or InverseDataBinding.

How to Use

SpinnerExtensions entails extension helper functions for Spinner that can be called from BindingAdapter.

    /**
     * set spinner entries
     */
    fun Spinner.setSpinnerEntries(entries: List<Any>?) {
        // ...
    }

    /**
     * set spinner onItemSelectedListener listener
     */
    fun Spinner.setSpinnerItemSelectedListener(listener: ItemSelectedListener?) {
        // ...
    }

    /**
     * set spinner onItemSelectedListener listener
     */
    fun Spinner.setSpinnerInverseBindingListener(listener: InverseBindingListener?) {
        // ...
    }

    /**
     * set spinner value
     */
    fun Spinner.setSpinnerValue(value: Any?) {
        // ...
    }

    /**
     * get spinner value
     */
    fun Spinner.getSpinnerValue(): Any? {
        // ...
    }

How to Bind

In your Gradle

    dataBinding {
        enabled = true
    }

Using DataBinding

In your BindingAdapter

    @BindingAdapter("entries")
    fun Spinner.setEntries(entries: List<Any>?) {
        setSpinnerEntries(entries)
    }

    @BindingAdapter("onItemSelected")
    fun Spinner.setItemSelectedListener(itemSelectedListener: ItemSelectedListener?) {
        setSpinnerItemSelectedListener(itemSelectedListener)
    }

    @BindingAdapter("newValue")
    fun Spinner.setNewValue(newValue: Any?) {
        setSpinnerValue(newValue)
    }

In your XML file

<Spinner
    android:id="@+id/item_quantity_spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:background="@drawable/ic_spinner_bg"
    android:gravity="center_vertical"
    android:paddingStart="4dp"
    android:textSize="16sp"
    app:entries="@{model.itemQuantityEntries}"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toEndOf="@id/item_image"
    app:newValue="@{model.itemQuantity}"
    app:onItemSelected="@{(newValue) -> presenter.onItemQuantityChange(model.cartItem, Converter.toInt(newValue))}" />

Using InverseDataBinding

In your BindingAdapter

    @BindingAdapter("selectedValue")
    fun Spinner.setSelectedValue(selectedValue: Any?) {
        setSpinnerValue(selectedValue)
    }

    @BindingAdapter("selectedValueAttrChanged")
    fun Spinner.setInverseBindingListener(inverseBindingListener: InverseBindingListener?) {
        setSpinnerInverseBindingListener(inverseBindingListener)
    }

    companion object InverseSpinnerBindings {

        @JvmStatic
        @InverseBindingAdapter(attribute = "selectedValue", event = "selectedValueAttrChanged")
        fun Spinner.getSelectedValue(): Any? {
            return getSpinnerValue()
        }
    }

In your XML file

<Spinner
    android:id="@+id/item_quantity_spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:background="@drawable/ic_spinner_bg"
    android:gravity="center_vertical"
    android:paddingStart="4dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:textSize="16sp"
    app:entries="@{model.itemQuantityEntries}"
    app:layout_constraintStart_toEndOf="@id/item_image"
    app:selectedValue="@={model.itemQuantity}" />

Library used

Add Android DataBinding Compiler your app gradle file.

    dependencies {
        kapt 'com.android.databinding:compiler:3.0.1'
    }

Make sure to apply kotlin-kapt plugin on top of your app gradle file.

    apply plugin: 'kotlin-kapt'

Also try

Reference

License

Copyright (c) 2018 Chetan Sachdeva

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Releases

No releases published

Packages

No packages published

Languages