| /* |
| * Copyright (C) 2015 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package androidx.databinding.adapters; |
| |
| import androidx.databinding.BindingAdapter; |
| import androidx.databinding.BindingMethod; |
| import androidx.databinding.BindingMethods; |
| import androidx.databinding.InverseBindingListener; |
| import androidx.databinding.InverseBindingMethod; |
| import androidx.databinding.InverseBindingMethods; |
| import androidx.annotation.RestrictTo; |
| import android.view.View; |
| import android.widget.Adapter; |
| import android.widget.AdapterView; |
| import android.widget.AdapterView.OnItemSelectedListener; |
| |
| /** |
| * @hide |
| */ |
| @RestrictTo(RestrictTo.Scope.LIBRARY) |
| @SuppressWarnings({"WeakerAccess", "unused", "unchecked", "rawtypes"}) |
| @BindingMethods({ |
| @BindingMethod(type = AdapterView.class, attribute = "android:onItemClick", method = "setOnItemClickListener"), |
| @BindingMethod(type = AdapterView.class, attribute = "android:onItemLongClick", method = "setOnItemLongClickListener"), |
| }) |
| @InverseBindingMethods({ |
| @InverseBindingMethod(type = AdapterView.class, attribute = "android:selectedItemPosition"), |
| @InverseBindingMethod(type = AdapterView.class, attribute = "android:selection", |
| method = "getSelectedItemPosition", |
| event = "android:selectedItemPositionAttrChanged"), |
| }) |
| public class AdapterViewBindingAdapter { |
| |
| @BindingAdapter("android:selectedItemPosition") |
| public static void setSelectedItemPosition(AdapterView view, int position) { |
| if (view.getSelectedItemPosition() != position) { |
| view.setSelection(position); |
| } |
| } |
| |
| @BindingAdapter("android:selection") |
| public static void setSelection(AdapterView view, int position) { |
| setSelectedItemPosition(view, position); |
| } |
| |
| @BindingAdapter({"android:selectedItemPosition", "android:adapter"}) |
| public static void setSelectedItemPosition(AdapterView view, int position, Adapter adapter) { |
| if (adapter != view.getAdapter()) { |
| //noinspection unchecked |
| view.setAdapter(adapter); |
| view.setSelection(position); |
| } else if (view.getSelectedItemPosition() != position) { |
| view.setSelection(position); |
| } |
| } |
| |
| @BindingAdapter({"android:selection", "android:adapter"}) |
| public static void setSelection(AdapterView view, int position, Adapter adapter) { |
| setSelectedItemPosition(view, position, adapter); |
| } |
| |
| @BindingAdapter(value = {"android:onItemSelected", "android:onNothingSelected", |
| "android:selectedItemPositionAttrChanged" }, requireAll = false) |
| public static void setOnItemSelectedListener(AdapterView view, final OnItemSelected selected, |
| final OnNothingSelected nothingSelected, final InverseBindingListener attrChanged) { |
| if (selected == null && nothingSelected == null && attrChanged == null) { |
| view.setOnItemSelectedListener(null); |
| } else { |
| view.setOnItemSelectedListener( |
| new OnItemSelectedComponentListener(selected, nothingSelected, attrChanged)); |
| } |
| } |
| |
| public static class OnItemSelectedComponentListener implements OnItemSelectedListener { |
| private final OnItemSelected mSelected; |
| private final OnNothingSelected mNothingSelected; |
| private final InverseBindingListener mAttrChanged; |
| |
| public OnItemSelectedComponentListener(OnItemSelected selected, |
| OnNothingSelected nothingSelected, InverseBindingListener attrChanged) { |
| this.mSelected = selected; |
| this.mNothingSelected = nothingSelected; |
| this.mAttrChanged = attrChanged; |
| } |
| |
| @Override |
| public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
| if (mSelected != null) { |
| mSelected.onItemSelected(parent, view, position, id); |
| } |
| if (mAttrChanged != null) { |
| mAttrChanged.onChange(); |
| } |
| } |
| |
| @Override |
| public void onNothingSelected(AdapterView<?> parent) { |
| if (mNothingSelected != null) { |
| mNothingSelected.onNothingSelected(parent); |
| } |
| if (mAttrChanged != null) { |
| mAttrChanged.onChange(); |
| } |
| } |
| } |
| |
| public interface OnItemSelected { |
| void onItemSelected(AdapterView<?> parent, View view, int position, long id); |
| } |
| |
| public interface OnNothingSelected { |
| void onNothingSelected(AdapterView<?> parent); |
| } |
| } |