Sunday, 29 May 2016

This week 7/2016

This week I finished configure fragments in my simple application. As I mentioned that a filed marked as a number field is associated with a String type of model attribute. If you need to associate your field with other time it is required to create your own binder method.

A long time took me to find out how to make unusual binder method for spinner.  In my case, I needed to bind a view action to a model. I used standard spinner listener to update my model but it wasn't a final solution. The best was to create your own binding method with a special annotations contains used in XML parameters. It is possible to bind more than one parameter at once.

ex.

    @BindingAdapter({"bind:enumElements", "bind:selected"})
    public static <T> void spinnerEnumBinnder(Spinner view, Enum<?>[] enumClass, T selected) {
        if (view.getAdapter() == null) {
            loadEnums(view, enumClass);
        }
        .....
    }


And than it is needed to add in layout XML:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:bind="http://schemas.android.com/apk/res-auto">
    <data>
        <variable name="linkName" type="org.example.ModelObject" />
    </data>

    <LinearLayout android:orientation="vertical" tools:context="org.example.ParamFragment">
        <Spinner
            android:id="@+id/objectType"
            bind:selected="@{linkName.type}"
            bind:enumElements="@{linkName.deviceTypes}"
            android:layout_gravity="center_horizontal|top"
            android:spinnerMode="dropdown" />

    </LinearLayout>
</layout>

No comments:

Post a Comment