Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if a listview is scrolling up or down in android?

Is there a way to identify if listview is being scroll up or down?

OnScrollListener doens't help me in this case.

Thanks in advance!

like image 782
dor506 Avatar asked Mar 09 '12 06:03

dor506


People also ask

How do I know if my scroll is up or down?

In this tutorial, we will show you how to detect whether you scroll up or down using jQuery which will allow you to perform an action based on the direction of scrolling. $(function(){ var lastScrollTop = 0, delta = 5; $(window). scroll(function(){ var nowScrollTop = $(this). scrollTop(); if(Math.

Does ListView have scroll?

A ListView in Android is a scrollable list used to display items.

Is ListView scrollable by default?

scrollable Boolean|String (default: false) If set to true the listview will display a scrollbar when the content exceeds the listview height value. By default scrolling is disabled. It could be also set to endless in order to enable the endless scrolling functionality.


2 Answers

this is a simple implementation:

lv.setOnScrollListener(new OnScrollListener() {

        private int mLastFirstVisibleItem;

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState){}

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {

            if(mLastFirstVisibleItem < firstVisibleItem){
                // Scrolling down
            }
            if(mLastFirstVisibleItem > firstVisibleItem){
                // scrolling up
            }
            mLastFirstVisibleItem = firstVisibleItem;
        }
    });
like image 129
Gal Rom Avatar answered Sep 30 '22 23:09

Gal Rom


There is a method in ScrollViews that reports the shifting of scrolls. It is called onScrollChanged(). However, it is declared protected, so you must create a wrapper class to gain access. For the usage of the method, please check android docs.

First, create an interface to expose the protected method

public interface OnScrollListener {
void onScrollChanged(int x, int y, int oldx, int oldy);
}

Then, create your wrapper and extend ScrollView

public class ReportingScrollView extends ScrollView {
private OnScrollListener onScrollListener = null;

public ReportingScrollView(Context context) {
    super(context);
}

public ReportingScrollView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public ReportingScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void setOnScrollListener(OnScrollListener onScrollListener) {
    this.onScrollListener = onScrollListener;
}

@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
    super.onScrollChanged(x, y, oldx, oldy);
    if (onScrollListener != null) {
        onScrollListener.onScrollChanged(x, y, oldx, oldy);
    }
}
}

Finally, include it in your XML layout like a custom view, and attach listeners like usual.

<your.package.ReportingScrollView />

scrollingandtrolling.setOnScrollListener(new OnScrollListener() {...});
like image 43
Some Noob Student Avatar answered Sep 30 '22 22:09

Some Noob Student