Skip to content

Instantly share code, notes, and snippets.

@marteinn
Last active October 10, 2015 14:17
Show Gist options
  • Select an option

  • Save marteinn/3702758 to your computer and use it in GitHub Desktop.

Select an option

Save marteinn/3702758 to your computer and use it in GitHub Desktop.
InteractiveScrollView - Understanding when ScrollView has reached the bottom
package se.marteinn.ui;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;
public class InteractiveScrollView extends ScrollView {
OnBottomReachedListener onBottomReachedListener;
public InteractiveScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public InteractiveScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public InteractiveScrollView(Context context) {
super(context);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
View view = (View) getChildAt(getChildCount()-1);
int diff = (view.getBottom()-(getHeight()+getScrollY()));
if( diff == 0 ){
// validate if we have a listener
if( onBottomReachedListener != null ){
// trigger the listener
onBottomReachedListener.onBottomReached();
}
}
super.onScrollChanged(l, t, oldl, oldt);
}
public OnBottomReachedListener getOnBottomReachedListener() {
return onBottomReachedListener;
}
public void setOnBottomReachedListener(
OnBottomReachedListener onBottomReachedListener) {
this.onBottomReachedListener = onBottomReachedListener;
}
public interface OnBottomReachedListener{
public void onBottomReached();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment