Android BindingAdapter执行顺序?

前端之家收集整理的这篇文章主要介绍了Android BindingAdapter执行顺序?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要了解数据绑定库如何确定其BindingAdapter的执行顺序.如果我有一个View的两个BindingAdapter,如果View有两个属性对应于那些BindingAdapters,它将如何确定首先执行哪个适配器?我问,因为执行顺序在我的情况下很重要.

我有以下BindingAdapter / s:

public class SpinnerBindingAdapter {

    @BindingAdapter(value = {"entries"})
    public static void setEntries(Spinner spinner,List<? extends SpinnerItem> spinnerItems) {

        for (int i = 0; i < spinnerItems.size(); i++) {
            spinnerItems.get(i).setIndex(i);
        }
        ArrayAdapter<? extends SpinnerItem> adapter =
                new ArrayAdapter<>(spinner.getContext(),R.layout.spinner_item,spinnerItems);
        spinner.setAdapter(adapter);
    }

    @InverseBindingAdapter(attribute = "selectedItem",event = "selectedItemAttrChanged")
    public static Object getSelectedItem(Spinner spinner) {

        Object selectedItem = spinner.getSelectedItem();

        return selectedItem;
    }

    @BindingAdapter(value = {"selectedItem"})
    public static void setSelectedItem(Spinner spinner,SpinnerItem spinnerItem) {
        if (spinner.getAdapter() == null) {
            return;
        }
        // Other code omitted for simplicity
    }

    @BindingAdapter(value = {"selectedItemAttrChanged"},requireAll = false)
    public static void setOnItemSelectedListener(Spinner spinner,final InverseBindingListener selectedItemChange) {
        if (selectedItemChange == null) {
            spinner.setOnItemSelectedListener(null);
        } else {
            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> parent,View view,int position,long id) {
                    selectedItemChange.onChange();
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
        }
    }
}

这是我如何填充Spinner并设置选择:

<Spinner
    android:id="@+id/spinner_system_activity_edit_tracker_unit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="fill_horizontal"
    app:entries="@{DatabaseModel.queryForAll()}"
    app:selectedItem="@={object.selectedItem}"/>

DatabaseModel.queryForAll是一个静态方法,它查询数据库并返回一个对象列表,然后将其提供给BindingAdapter. BindingAdapter获取此列表,使用索引更新其每个项目,并将其设置为微调器的适配器.

无论出于何种原因,始终首先调用“setSelectedItem”BindingAdapter.这是不可取的,因为我需要首先初始化条目.如果先未初始化,则首次调用setSelectedItem时,spinner.getAdapter()将为null.这意味着以前保存的选择将无法恢复.

解决方法

Android数据绑定中无法保证执行顺序.因此,您应该合并依赖于多个属性的绑定适配器.在您的情况下,您需要合并selectedItem和条目的绑定适配器.
@BindingAdapter(value = {"selectedItem","entries"},requireAll = false)
public static void setSelectedItem(Spinner spinner,SpinnerItem spinnerItem,List<? extends SpinnerItem> spinnerItems) {
    // Set entries attribute when provided
    if (spinnerItems != null) {
        for (int i = 0; i < spinnerItems.size(); i++) {
            spinnerItems.get(i).setIndex(i);
        }
        ArrayAdapter<? extends SpinnerItem> adapter =
            new ArrayAdapter<>(spinner.getContext(),spinnerItems);
        spinner.setAdapter(adapter);
    }
    // set selectedItem attribute when provided
    if (spinnerItem != null) {
        if (spinner.getAdapter() == null) {
            return;
        }
        // Other code omitted for simplicity
    }
}
原文链接:https://www.f2er.com/android/308760.html

猜你在找的Android相关文章