• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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 
17 package com.android.inputmethod.keyboard.internal;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.widget.ScrollView;
22 
23 /**
24  * This is an extended {@link ScrollView} that can notify
25  * {@link ScrollView#onScrollChanged(int,int,int,int} and
26  * {@link ScrollView#onOverScrolled(int,int,int,int)} to a content view.
27  */
28 public class ScrollViewWithNotifier extends ScrollView {
29     private ScrollListener mScrollListener = EMPTY_LISTER;
30 
31     public interface ScrollListener {
notifyScrollChanged(int scrollX, int scrollY, int oldX, int oldY)32         public void notifyScrollChanged(int scrollX, int scrollY, int oldX, int oldY);
notifyOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY)33         public void notifyOverScrolled(int scrollX, int scrollY, boolean clampedX,
34                 boolean clampedY);
35     }
36 
37     private static final ScrollListener EMPTY_LISTER = new ScrollListener() {
38         @Override
39         public void notifyScrollChanged(int scrollX, int scrollY, int oldX, int oldY) {}
40         @Override
41         public void notifyOverScrolled(int scrollX, int scrollY, boolean clampedX,
42                 boolean clampedY) {}
43     };
44 
ScrollViewWithNotifier(final Context context, final AttributeSet attrs)45     public ScrollViewWithNotifier(final Context context, final AttributeSet attrs) {
46         super(context, attrs);
47     }
48 
49     @Override
onScrollChanged(final int scrollX, final int scrollY, final int oldX, final int oldY)50     protected void onScrollChanged(final int scrollX, final int scrollY, final int oldX,
51             final int oldY) {
52         super.onScrollChanged(scrollX, scrollY, oldX, oldY);
53         mScrollListener.notifyScrollChanged(scrollX, scrollY, oldX, oldY);
54     }
55 
56     @Override
onOverScrolled(final int scrollX, final int scrollY, final boolean clampedX, final boolean clampedY)57     protected void onOverScrolled(final int scrollX, final int scrollY, final boolean clampedX,
58             final boolean clampedY) {
59         super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
60         mScrollListener.notifyOverScrolled(scrollX, scrollY, clampedX, clampedY);
61     }
62 
setScrollListener(final ScrollListener listener)63     public void setScrollListener(final ScrollListener listener) {
64         mScrollListener = listener;
65     }
66 }
67