Skip to content

Instantly share code, notes, and snippets.

@PaNaVTEC
Last active August 29, 2015 14:05
Show Gist options
  • Save PaNaVTEC/1e352e9257d8e553ce86 to your computer and use it in GitHub Desktop.
Save PaNaVTEC/1e352e9257d8e553ce86 to your computer and use it in GitHub Desktop.
PullDown Android detector
import android.widget.AbsListView;
import android.widget.ListView;
/**
* Makes a listview to have a pull down, this is a wrapper.
* Is not a good idea to override a ListView because there
* are a lot of libraries and we dont know when we need one.
*/
public class PulldownListViewHelper {
private final static int ITEMS_BEFORE_LAST_TO_DETECT = 3;
private ListView listView;
private OnPullDownListener listener;
AbsListView.OnScrollListener scrolllistener = new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
final int lastItem = firstVisibleItem + visibleItemCount;
if (listener != null && lastItem >= (totalItemCount - ITEMS_BEFORE_LAST_TO_DETECT)) {
listener.onPullDown();
}
}
};
public PulldownListViewHelper(ListView listView, OnPullDownListener listener) {
this.listView = listView;
this.listener = listener;
}
public void initPulldown() {
listView.setOnScrollListener(scrolllistener);
}
public void stopPulldown() {
listView.setOnScrollListener(null);
}
public interface OnPullDownListener {
void onPullDown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment