Skip to content

Instantly share code, notes, and snippets.

@thuytrinh
Last active August 29, 2015 14:03
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 thuytrinh/23bd3e8240a43cab5e14 to your computer and use it in GitHub Desktop.
Save thuytrinh/23bd3e8240a43cab5e14 to your computer and use it in GitHub Desktop.
A StickyListHeadersListView that notifies us when we should load (or request) more data
import android.content.Context;
import android.util.AttributeSet;
import android.widget.AbsListView;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
public class RequestMoreListView extends StickyListHeadersListView implements AbsListView.OnScrollListener {
private OnRequestMoreListener mOnRequestMoreListener;
private boolean mIsRequesting;
private AbsListView.OnScrollListener mExternalOnScrollListener;
public RequestMoreListView(Context context) {
super(context);
initComponents();
}
public RequestMoreListView(Context context, AttributeSet attrs) {
super(context, attrs);
initComponents();
}
public RequestMoreListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initComponents();
}
/**
* Register a callback to be invoked when the ListView reaches the last item
*/
public void setOnRequestMoreListener(OnRequestMoreListener onRequestMoreListener) {
mOnRequestMoreListener = onRequestMoreListener;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (mExternalOnScrollListener != null) {
mExternalOnScrollListener.onScrollStateChanged(view, scrollState);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (mExternalOnScrollListener != null) {
mExternalOnScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
}
// We only proceed when the registered callback is available
if (mOnRequestMoreListener != null && !mIsRequesting) {
// We expect that, the ListView overflows with the items
if (totalItemCount > visibleItemCount &&
// And we've reached the last item
(firstVisibleItem + visibleItemCount) >= totalItemCount) {
setRequesting(true);
// Notify that we should request more data
mOnRequestMoreListener.onRequestMore();
}
}
}
/**
* @return Whether requesting is underway
*/
public boolean isRequesting() {
return mIsRequesting;
}
/**
* @param isRequesting If true, we don't expect to be notified by the OnRequestMoreListener callback.
* Otherwise, the ListView should notify us when we should request more data.
*/
public void setRequesting(boolean isRequesting) {
mIsRequesting = isRequesting;
}
@Override
public void setOnScrollListener(AbsListView.OnScrollListener onScrollListener) {
super.setOnScrollListener(this);
if (this != onScrollListener) {
mExternalOnScrollListener = onScrollListener;
}
}
private void initComponents() {
if (isInEditMode()) {
return;
}
// Use an internal OnScrollListener to watch scrolling on the ListView.
setOnScrollListener(this);
}
/**
* A callback to be invoked when the ListView reaches the last item
*/
public interface OnRequestMoreListener {
void onRequestMore();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment