-
-
Save redleafar/c251b5819f88e0f1e442f671293a339c to your computer and use it in GitHub Desktop.
ScrollListView - Detect bottom reached for ListView
This file contains hidden or 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
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