ObservableScrollView.java revision fc1a824ec1453a84d9c53837df4ab93eca591a5f
1/* 2 * Copyright (C) 2014 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 17package com.android.systemui.statusbar.phone; 18 19import android.content.Context; 20import android.util.AttributeSet; 21import android.view.View; 22import android.widget.ScrollView; 23 24/** 25 * A scroll view which can be observed for scroll change events. 26 */ 27public class ObservableScrollView extends ScrollView { 28 29 private Listener mListener; 30 private int mLastOverscrollAmount; 31 32 public ObservableScrollView(Context context, AttributeSet attrs) { 33 super(context, attrs); 34 } 35 36 public void setListener(Listener listener) { 37 mListener = listener; 38 } 39 40 public boolean isScrolledToBottom() { 41 return getScrollY() == getMaxScrollY(); 42 } 43 44 private int getMaxScrollY() { 45 int scrollRange = 0; 46 if (getChildCount() > 0) { 47 View child = getChildAt(0); 48 scrollRange = Math.max(0, 49 child.getHeight() - (getHeight() - mPaddingBottom - mPaddingTop)); 50 } 51 return scrollRange; 52 } 53 54 @Override 55 protected void onScrollChanged(int l, int t, int oldl, int oldt) { 56 super.onScrollChanged(l, t, oldl, oldt); 57 if (mListener != null) { 58 mListener.onScrollChanged(); 59 } 60 } 61 62 @Override 63 protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, 64 int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, 65 boolean isTouchEvent) { 66 mLastOverscrollAmount = Math.max(0, scrollY + deltaY - getMaxScrollY()); 67 return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, 68 maxOverScrollX, maxOverScrollY, isTouchEvent); 69 } 70 71 @Override 72 protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) { 73 super.onOverScrolled(scrollX, scrollY, clampedX, clampedY); 74 if (mListener != null && mLastOverscrollAmount > 0) { 75 mListener.onOverscrolled(mLastOverscrollAmount); 76 } 77 } 78 79 public interface Listener { 80 void onScrollChanged(); 81 void onOverscrolled(int amount); 82 } 83} 84