Skip to content

Instantly share code, notes, and snippets.

@cesco89
Created May 11, 2014 13:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cesco89/02cdc3182dfc6ab992f7 to your computer and use it in GitHub Desktop.
Save cesco89/02cdc3182dfc6ab992f7 to your computer and use it in GitHub Desktop.
ListView with detectable scrolling direction
list.setOnDetectScrollListener(new OnDetectScrollListener() {
@Override
public void onUpScrolling() {
// TODO Auto-generated method stub
mBottom.setVisibility(View.VISIBLE);
}
@Override
public void onDownScrolling() {
// TODO Auto-generated method stub
mBottom.setVisibility(View.GONE);
}
@Override
public void onBottomScrolled() {
// TODO Auto-generated method stub
mBottom.setVisibility(View.VISIBLE);
}
@Override
public void onTopScrolled() {
// TODO Auto-generated method stub
mBottom.setVisibility(View.VISIBLE);
}
});
public interface OnDetectScrollListener {
void onUpScrolling();
void onDownScrolling();
void onBottomScrolled();
void onTopScrolled();
}
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
public class ScrollDetectableListView extends android.widget.ListView {
private OnScrollListener onScrollListener;
private OnDetectScrollListener onDetectScrollListener;
public ScrollDetectableListView(Context context) {
super(context);
onCreate(context, null, null);
}
public ScrollDetectableListView(Context context, AttributeSet attrs) {
super(context, attrs);
onCreate(context, attrs, null);
}
public ScrollDetectableListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
onCreate(context, attrs, defStyle);
}
@SuppressWarnings("UnusedParameters")
private void onCreate(Context context, AttributeSet attrs, Integer defStyle) {
setListeners();
}
private void setListeners() {
super.setOnScrollListener(new OnScrollListener() {
private int oldTop;
private int oldFirstVisibleItem;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (onScrollListener != null) {
onScrollListener.onScrollStateChanged(view, scrollState);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (onScrollListener != null) {
onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
}
if (onDetectScrollListener != null) {
onDetectedListScroll(view, firstVisibleItem, totalItemCount, visibleItemCount);
}
}
private void onDetectedListScroll(AbsListView absListView, int firstVisibleItem, int totalItemCount, int visibleItemCount) {
View view = absListView.getChildAt(0);
int top = (view == null) ? 0 : view.getTop();
int lastItem = firstVisibleItem + visibleItemCount;
if (firstVisibleItem == oldFirstVisibleItem) {
if (top > oldTop) {
onDetectScrollListener.onUpScrolling();
} else if (top < oldTop) {
onDetectScrollListener.onDownScrolling();
}
} else {
if (firstVisibleItem < oldFirstVisibleItem) {
onDetectScrollListener.onUpScrolling();
} else {
onDetectScrollListener.onDownScrolling();
}
}
if(lastItem == totalItemCount) {
if(oldFirstVisibleItem <= lastItem){
onDetectScrollListener.onBottomScrolled();
}else {
onDetectScrollListener.onUpScrolling();
}
}
oldTop = top;
oldFirstVisibleItem = firstVisibleItem;
}
});
}
@Override
public void setOnScrollListener(OnScrollListener onScrollListener) {
this.onScrollListener = onScrollListener;
}
public void setOnDetectScrollListener(OnDetectScrollListener onDetectScrollListener) {
this.onDetectScrollListener = onDetectScrollListener;
}
}
@mehmetyilmaz001
Copy link

Hi, is it possible to handle on scrolled up / on scrolled down event? onUpScrolling and onDowScrolling are on-going events so when we need to call individual action to do something these events suck. For example I want to hide / show something when scroll up / down started. See on this video what happens when use on scrolling up / downd events https://www.youtube.com/watch?v=U7KNwS6JlUk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment