AbsListViewBindingAdapter.java revision 716ba89e7f459f49ea85070d4710c1d79d715298
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.widget.AbsListView;
22import android.widget.AbsListView.OnScrollListener;
23
24@BindingMethods({
25        @BindingMethod(type = AbsListView.class, attribute = "android:listSelector", method = "setSelector"),
26        @BindingMethod(type = AbsListView.class, attribute = "android:scrollingCache", method = "setScrollingCacheEnabled"),
27        @BindingMethod(type = AbsListView.class, attribute = "android:smoothScrollbar", method = "setSmoothScrollbarEnabled"),
28        @BindingMethod(type = AbsListView.class, attribute = "android:onMovedToScrapHeap", method = "setRecyclerListener"),
29})
30public class AbsListViewBindingAdapter {
31
32    @BindingAdapter("android:onScroll")
33    public static void setOnScroll(AbsListView view, OnScroll listener) {
34        setOnScroll(view, listener, null);
35    }
36
37    @BindingAdapter("android:onScrollStateChanged")
38    public static void setOnScroll(AbsListView view, OnScrollStateChanged listener) {
39        setOnScroll(view, null, listener);
40    }
41
42    @BindingAdapter({"android:onScroll", "android:onScrollStateChanged"})
43    public static void setOnScroll(AbsListView view, final OnScroll scrollListener,
44            final OnScrollStateChanged scrollStateListener) {
45        view.setOnScrollListener(new OnScrollListener() {
46            @Override
47            public void onScrollStateChanged(AbsListView view, int scrollState) {
48                if (scrollStateListener != null) {
49                    scrollStateListener.onScrollStateChanged(view, scrollState);
50                }
51            }
52
53            @Override
54            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
55                    int totalItemCount) {
56                if (scrollListener != null) {
57                    scrollListener
58                            .onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
59                }
60            }
61        });
62    }
63
64    public interface OnScroll {
65        void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
66                int totalItemCount);
67    }
68
69    public interface OnScrollStateChanged {
70        void onScrollStateChanged(AbsListView view, int scrollState);
71    }
72}
73