AdapterViewBindingAdapter.java revision 96b22e7bbbf942aea1079dc8e8d0c4657663e5a7
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 */
16package android.databinding.adapters;
17
18import android.databinding.BindingAdapter;
19import android.databinding.BindingMethod;
20import android.databinding.BindingMethods;
21import android.view.View;
22import android.widget.AdapterView;
23import android.widget.AdapterView.OnItemSelectedListener;
24
25@BindingMethods({
26        @BindingMethod(type = AdapterView.class, attribute = "android:onItemClick", method = "setOnItemClickListener"),
27        @BindingMethod(type = AdapterView.class, attribute = "android:onItemLongClick", method = "setOnItemLongClickListener"),
28})
29public class AdapterViewBindingAdapter {
30
31    @BindingAdapter(value = {"android:onItemSelected", "android:onNothingSelected"},
32            requireAll = false)
33    public static void setOnItemSelectedListener(AdapterView view, final OnItemSelected selected,
34            final OnNothingSelected nothingSelected) {
35        if (selected == null && nothingSelected == null) {
36            view.setOnItemSelectedListener(null);
37        } else {
38            view.setOnItemSelectedListener(
39                    new OnItemSelectedComponentListener(selected, nothingSelected));
40        }
41    }
42
43    public static class OnItemSelectedComponentListener implements OnItemSelectedListener {
44        private final OnItemSelected mSelected;
45        private final OnNothingSelected mNothingSelected;
46
47        public OnItemSelectedComponentListener(OnItemSelected selected,
48                OnNothingSelected nothingSelected) {
49            this.mSelected = selected;
50            this.mNothingSelected = nothingSelected;
51        }
52        @Override
53        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
54            if (mSelected != null) {
55                mSelected.onItemSelected(parent, view, position, id);
56            }
57        }
58
59        @Override
60        public void onNothingSelected(AdapterView<?> parent) {
61            if (mNothingSelected != null) {
62                mNothingSelected.onNothingSelected(parent);
63            }
64        }
65    }
66
67    public interface OnItemSelected {
68        void onItemSelected(AdapterView<?> parent, View view, int position, long id);
69    }
70
71    public interface OnNothingSelected {
72        void onNothingSelected(AdapterView<?> parent);
73    }
74}
75