AbsListViewBindingAdapter.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.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(value = {"android:onScroll", "android:onScrollStateChanged"},
33            requireAll = false)
34    public static void setOnScroll(AbsListView view, final OnScroll scrollListener,
35            final OnScrollStateChanged scrollStateListener) {
36        view.setOnScrollListener(new OnScrollListener() {
37            @Override
38            public void onScrollStateChanged(AbsListView view, int scrollState) {
39                if (scrollStateListener != null) {
40                    scrollStateListener.onScrollStateChanged(view, scrollState);
41                }
42            }
43
44            @Override
45            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
46                    int totalItemCount) {
47                if (scrollListener != null) {
48                    scrollListener
49                            .onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
50                }
51            }
52        });
53    }
54
55    public interface OnScroll {
56        void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
57                int totalItemCount);
58    }
59
60    public interface OnScrollStateChanged {
61        void onScrollStateChanged(AbsListView view, int scrollState);
62    }
63}
64