Skip to content

Instantly share code, notes, and snippets.

@Croydon
Forked from marteinn/InteractiveScrollView.java
Created November 16, 2017 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Croydon/3b9bb513db684c282f5eb451c267c457 to your computer and use it in GitHub Desktop.
Save Croydon/3b9bb513db684c282f5eb451c267c457 to your computer and use it in GitHub Desktop.
ScrollView with a OnBottomReachedListener for Android
package se.marteinn.ui;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;
/**
* Triggers a event when scrolling reaches bottom.
*
* Created by martinsandstrom on 2010-05-12.
* Updated by martinsandstrom on 2014-07-22.
*
* Usage:
*
* scrollView.setOnBottomReachedListener(
* new InteractiveScrollView.OnBottomReachedListener() {
* @Override
* public void onBottomReached() {
* // do something
* }
* }
* );
*
*
* Include in layout:
*
* <se.marteinn.ui.InteractiveScrollView
* android:layout_width="match_parent"
* android:layout_height="match_parent" />
*
*/
public class InteractiveScrollView extends ScrollView {
OnBottomReachedListener mListener;
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 && mListener != null) {
mListener.onBottomReached();
}
super.onScrollChanged(l, t, oldl, oldt);
}
// Getters & Setters
public OnBottomReachedListener getOnBottomReachedListener() {
return mListener;
}
public void setOnBottomReachedListener(
OnBottomReachedListener onBottomReachedListener) {
mListener = onBottomReachedListener;
}
/**
* Event listener.
*/
public interface OnBottomReachedListener{
public void onBottomReached();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment