Skip to content

Instantly share code, notes, and snippets.

@JcMinarro
Forked from PaNaVTEC/PulldownListViewHelper
Created August 13, 2014 09:42
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 JcMinarro/80d68a170806ec9816b3 to your computer and use it in GitHub Desktop.
Save JcMinarro/80d68a170806ec9816b3 to your computer and use it in GitHub Desktop.
import android.util.Log;
import android.widget.AbsListView;
import android.widget.ListView;
import org.jetbrains.annotations.NotNull;
/**
* 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;
/**
* ListView to play with
*/
private ListView listView;
/**
* OnPullDownListener
*/
private OnPullDownListener listener;
/**
* Listener of scroll view that makes the list to attend to pull down events
*/
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) {
Log.d("PulldownListViewHelper", "FVI: " + firstVisibleItem + ", VIC:" +visibleItemCount+ ", TIC:" + totalItemCount);
final int lastItem = firstVisibleItem + visibleItemCount;
if(listener != null && lastItem >= (totalItemCount - ITEMS_BEFORE_LAST_TO_DETECT)) {
listener.onPullDown();
}
}
};
/**
* Default constructor
* @param listView the listview to check pulldowns
* @param listener the listener to attend pulldown events
*/
public PulldownListViewHelper(@NotNull ListView listView, @NotNull OnPullDownListener listener) {
this.listView = listView;
this.listener = listener;
}
/**
* Inits pull down listening
*/
public void initPulldown() {
listView.setOnScrollListener(scrolllistener);
}
/**
* Stops pull down listening
*/
public void stopPulldown() {
listView.setOnScrollListener(null);
}
/**
* Simple interface for notify pull down events
*/
public interface OnPullDownListener {
/**
* Method to notify pull down events
*/
void onPullDown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment