Last active
August 29, 2015 14:03
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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