1 /*
2  * Copyright (C) 2014 Google Inc.
3  * Licensed to The Android Open Source Project.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.android.mail.ui;
19 
20 import android.content.Context;
21 import android.database.Observable;
22 import android.util.AttributeSet;
23 import android.widget.ListView;
24 
25 import com.android.mail.browse.ScrollNotifier;
26 
27 /**
28  * Ordinary list view with extra boilerplate to notify on scrollbar-related events (unrelated to
29  * {@link android.widget.AbsListView.OnScrollListener}!)
30  */
31 public class ScrollNotifyingListView extends ListView implements ScrollNotifier {
32 
33     private final ScrollObservable mObservable = new ScrollObservable();
34 
ScrollNotifyingListView(Context c)35     public ScrollNotifyingListView(Context c) {
36         this(c, null);
37     }
38 
ScrollNotifyingListView(Context c, AttributeSet attrs)39     public ScrollNotifyingListView(Context c, AttributeSet attrs) {
40         super(c, attrs);
41     }
42 
43     @Override
addScrollListener(ScrollListener l)44     public void addScrollListener(ScrollListener l) {
45         mObservable.registerObserver(l);
46     }
47 
48     @Override
removeScrollListener(ScrollListener l)49     public void removeScrollListener(ScrollListener l) {
50         mObservable.unregisterObserver(l);
51     }
52 
53     @Override
onScrollChanged(int l, int t, int oldl, int oldt)54     protected void onScrollChanged(int l, int t, int oldl, int oldt) {
55         super.onScrollChanged(l, t, oldl, oldt);
56         mObservable.onScrollChanged(l, t, oldl, oldt);
57     }
58 
59     @Override
computeVerticalScrollRange()60     public int computeVerticalScrollRange() {
61         return super.computeVerticalScrollRange();
62     }
63 
64     @Override
computeVerticalScrollOffset()65     public int computeVerticalScrollOffset() {
66         return super.computeVerticalScrollOffset();
67     }
68 
69     @Override
computeVerticalScrollExtent()70     public int computeVerticalScrollExtent() {
71         return super.computeVerticalScrollExtent();
72     }
73 
74     @Override
computeHorizontalScrollRange()75     public int computeHorizontalScrollRange() {
76         return super.computeHorizontalScrollRange();
77     }
78 
79     @Override
computeHorizontalScrollOffset()80     public int computeHorizontalScrollOffset() {
81         return super.computeHorizontalScrollOffset();
82     }
83 
84     @Override
computeHorizontalScrollExtent()85     public int computeHorizontalScrollExtent() {
86         return super.computeHorizontalScrollExtent();
87     }
88 
89     private static class ScrollObservable extends Observable<ScrollListener> {
90 
91         @SuppressWarnings("unused")
onScrollChanged(int l, int t, int oldl, int oldt)92         public void onScrollChanged(int l, int t, int oldl, int oldt) {
93             for (ScrollListener sl : mObservers) {
94                 sl.onNotifierScroll(t);
95             }
96         }
97 
98     }
99 
100 }
101