Debian Codegraph @

extensions/baseAdapters/src/main/java/android/databinding/adapters/AdapterViewBindingAdapter.java

raw
1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package android.databinding.adapters;
17
18 import android.databinding.BindingAdapter;
19 import android.databinding.BindingMethod;
20 import android.databinding.BindingMethods;
21 import android.databinding.InverseBindingListener;
22 import android.databinding.InverseBindingMethod;
23 import android.databinding.InverseBindingMethods;
24 import android.view.View;
25 import android.widget.AbsListView;
26 import android.widget.AdapterView;
27 import android.widget.AdapterView.OnItemSelectedListener;
28
29 @BindingMethods({
30 @BindingMethod(type = AdapterView.class, attribute = "android:onItemClick", method = "setOnItemClickListener"),
31 @BindingMethod(type = AdapterView.class, attribute = "android:onItemLongClick", method = "setOnItemLongClickListener"),
32 })
33 @InverseBindingMethods({
34 @InverseBindingMethod(type = AdapterView.class, attribute = "android:selectedItemPosition"),
35 })
36 public class AdapterViewBindingAdapter {
37
38 @BindingAdapter("android:selectedItemPosition")
39 public static void setSelectedItemPosition(AdapterView view, int position) {
40 if (view.getSelectedItemPosition() != position) {
41 view.setSelection(position);
42 }
43 }
44
45 @BindingAdapter(value = {"android:onItemSelected", "android:onNothingSelected",
46 "android:selectedItemPositionAttrChanged"}, requireAll = false)
47 public static void setOnItemSelectedListener(AdapterView view, final OnItemSelected selected,
48 final OnNothingSelected nothingSelected, final InverseBindingListener attrChanged) {
49 if (selected == null && nothingSelected == null && attrChanged == null) {
50 view.setOnItemSelectedListener(null);
51 } else {
52 view.setOnItemSelectedListener(
53 new OnItemSelectedComponentListener(selected, nothingSelected, attrChanged));
54 }
55 }
56
57 public static class OnItemSelectedComponentListener implements OnItemSelectedListener {
58 private final OnItemSelected mSelected;
59 private final OnNothingSelected mNothingSelected;
60 private final InverseBindingListener mAttrChanged;
61
62 public OnItemSelectedComponentListener(OnItemSelected selected,
63 OnNothingSelected nothingSelected, InverseBindingListener attrChanged) {
64 this.mSelected = selected;
65 this.mNothingSelected = nothingSelected;
66 this.mAttrChanged = attrChanged;
67 }
68
69 @Override
70 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
71 if (mSelected != null) {
72 mSelected.onItemSelected(parent, view, position, id);
73 }
74 if (mAttrChanged != null) {
75 mAttrChanged.onChange();
76 }
77 }
78
79 @Override
80 public void onNothingSelected(AdapterView<?> parent) {
81 if (mNothingSelected != null) {
82 mNothingSelected.onNothingSelected(parent);
83 }
84 if (mAttrChanged != null) {
85 mAttrChanged.onChange();
86 }
87 }
88 }
89
90 public interface OnItemSelected {
91 void onItemSelected(AdapterView<?> parent, View view, int position, long id);
92 }
93
94 public interface OnNothingSelected {
95 void onNothingSelected(AdapterView<?> parent);
96 }
97 }