diff options
Diffstat (limited to 'library/main/src/com/android/setupwizardlib/view/BottomScrollView.java')
-rw-r--r-- | library/main/src/com/android/setupwizardlib/view/BottomScrollView.java | 131 |
1 files changed, 68 insertions, 63 deletions
diff --git a/library/main/src/com/android/setupwizardlib/view/BottomScrollView.java b/library/main/src/com/android/setupwizardlib/view/BottomScrollView.java index eeb40a9..962538f 100644 --- a/library/main/src/com/android/setupwizardlib/view/BottomScrollView.java +++ b/library/main/src/com/android/setupwizardlib/view/BottomScrollView.java @@ -17,12 +17,11 @@ package com.android.setupwizardlib.view; import android.content.Context; +import androidx.annotation.VisibleForTesting; import android.util.AttributeSet; import android.view.View; import android.widget.ScrollView; -import androidx.annotation.VisibleForTesting; - /** * An extension of ScrollView that will invoke a listener callback when the ScrollView needs * scrolling, and when the ScrollView is being scrolled to the bottom. This is often used in Setup @@ -30,75 +29,81 @@ import androidx.annotation.VisibleForTesting; */ public class BottomScrollView extends ScrollView { - public interface BottomScrollListener { - void onScrolledToBottom(); - void onRequiresScroll(); - } + public interface BottomScrollListener { + void onScrolledToBottom(); + + void onRequiresScroll(); + } - private BottomScrollListener mListener; - private int mScrollThreshold; - private boolean mRequiringScroll = false; + private BottomScrollListener listener; + private int scrollThreshold; + private boolean requiringScroll = false; - private final Runnable mCheckScrollRunnable = new Runnable() { + private final Runnable checkScrollRunnable = + new Runnable() { @Override public void run() { - checkScroll(); + checkScroll(); } - }; - - public BottomScrollView(Context context) { - super(context); + }; + + public BottomScrollView(Context context) { + super(context); + } + + public BottomScrollView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public BottomScrollView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + public void setBottomScrollListener(BottomScrollListener l) { + listener = l; + } + + @VisibleForTesting + public BottomScrollListener getBottomScrollListener() { + return listener; + } + + @VisibleForTesting + public int getScrollThreshold() { + return scrollThreshold; + } + + @Override + protected void onLayout(boolean changed, int l, int t, int r, int b) { + super.onLayout(changed, l, t, r, b); + final View child = getChildAt(0); + if (child != null) { + scrollThreshold = Math.max(0, child.getMeasuredHeight() - b + t - getPaddingBottom()); } - - public BottomScrollView(Context context, AttributeSet attrs) { - super(context, attrs); + if (b - t > 0) { + // Post check scroll in the next run loop, so that the callback methods will be invoked + // after the layout pass. This way a new layout pass will be scheduled if view + // properties are changed in the callbacks. + post(checkScrollRunnable); } + } - public BottomScrollView(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); + @Override + protected void onScrollChanged(int l, int t, int oldl, int oldt) { + super.onScrollChanged(l, t, oldl, oldt); + if (oldt != t) { + checkScroll(); } - - public void setBottomScrollListener(BottomScrollListener l) { - mListener = l; + } + + private void checkScroll() { + if (listener != null) { + if (getScrollY() >= scrollThreshold) { + listener.onScrolledToBottom(); + } else if (!requiringScroll) { + requiringScroll = true; + listener.onRequiresScroll(); + } } - - @VisibleForTesting - public int getScrollThreshold() { - return mScrollThreshold; - } - - @Override - protected void onLayout(boolean changed, int l, int t, int r, int b) { - super.onLayout(changed, l, t, r, b); - final View child = getChildAt(0); - if (child != null) { - mScrollThreshold = Math.max(0, child.getMeasuredHeight() - b + t - getPaddingBottom()); - } - if (b - t > 0) { - // Post check scroll in the next run loop, so that the callback methods will be invoked - // after the layout pass. This way a new layout pass will be scheduled if view - // properties are changed in the callbacks. - post(mCheckScrollRunnable); - } - } - - @Override - protected void onScrollChanged(int l, int t, int oldl, int oldt) { - super.onScrollChanged(l, t, oldl, oldt); - if (oldt != t) { - checkScroll(); - } - } - - private void checkScroll() { - if (mListener != null) { - if (getScrollY() >= mScrollThreshold) { - mListener.onScrolledToBottom(); - } else if (!mRequiringScroll) { - mRequiringScroll = true; - mListener.onRequiresScroll(); - } - } - } - + } } |