Skip to content

Instantly share code, notes, and snippets.

@alexrainman
Forked from marteinn/ScrollListView.java
Last active August 29, 2015 14:25
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 alexrainman/0165b886c0ac191b295a to your computer and use it in GitHub Desktop.
Save alexrainman/0165b886c0ac191b295a to your computer and use it in GitHub Desktop.
package se.marteinn.ui;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.AbsListView;
import android.widget.ListView;
/**
* Triggers a event when scrolling reaches bottom.
*
* Created by martinsandstrom on 2013-05-02.
*
* Usage:
*
* listView.setOnBottomReachedListener(
* new ScrollListView.OnBottomReachedListener() {
* @Override
* public void onBottomReached() {
* // do something
* }
* }
* );
*/
public class ScrollListView extends ListView implements AbsListView.OnScrollListener {
private OnBottomReachedListener mListener;
/**
* Scroll position offset value to trigger earlier bottom reached events.
*/
private int mOffset = 0;
public ScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
defineScrolling();
}
public ScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
defineScrolling();
}
public ScrollListView(Context context) {
super(context);
defineScrolling();
}
/**
* Defines scrolling behaviour by subscribing a scroll listener.
*/
private void defineScrolling() {
this.setOnScrollListener(this);
}
/**
* Removes internal scroll listener.
*/
public void reset() {
this.setOnScrollListener(null);
}
// Listeners
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
int position = firstVisibleItem+visibleItemCount;
int limit = totalItemCount - mOffset;
// Check if bottom has been reached
if (position >= limit && totalItemCount > 0) {
if (mListener != null ) {
mListener.onBottomReached();
}
}
}
// Getters & Setters
public OnBottomReachedListener getOnBottomReachedListener() {
return mListener;
}
public void setOnBottomReachedListener(
OnBottomReachedListener onBottomReachedListener) {
this.mListener = onBottomReachedListener;
}
public int getOffset() {
return mOffset;
}
public void setOffset(int offset) {
mOffset = offset;
}
/**
* 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