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

22,674

Solution 1

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;
        }
    });

Solution 2

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() {...});

Solution 3

for whom ever still looking for an answer for kotlin, this works for it

MainListView.setOnScrollListener(object :AbsListView.OnScrollListener {
     // var VisibleItem: Int = 0
      override fun onScroll(p0: AbsListView?, FirstVisibleItem: Int, i2: Int, i3: Int) {
      /*   
      if(VisibleItem < FirstVisibleItem)
      {
      Toast.makeText(applicationContext,"Scrolling Down",Toast.LENGTH_SHORT).show()
          fab.hide()
      }
      if(VisibleItem >FirstVisibleItem)
      {
          fab.show()
          Toast.makeText(applicationContext,"Scrolling Up",Toast.LENGTH_SHORT).show()
      }
          VisibleItem=FirstVisibleItem;
  */
      }
      override fun onScrollStateChanged(p0: AbsListView?, p1: Int) {
          if (p1 == 1) {
              fab.hide()
          } else {
              fab.show()
          }
      }

  })
}

Uncomment to use an alternative method.

Solution 4

An easy and working solution.

private int lastPosition = -1;

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
        {
            if(lastPosition == firstVisibleItem)
            {
                return;
            }

            if(firstVisibleItem > lastPosition)
            {
                Logger.print("Going Down");
            }
            else
            {
                Logger.print("Going Up");
            }

            lastPosition = firstVisibleItem;
        }
Share:
22,674
dor506
Author by

dor506

Updated on April 21, 2020

Comments

  • dor506
    dor506 about 4 years

    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!

  • Christopher Perry
    Christopher Perry over 11 years
    This only seems to be called when you fling the list, it's not repeatedly called.
  • Johann
    Johann almost 9 years
    Only useful once the top item is no longer the first item. Won't tell you direction if you scroll back and forth while the same item remains at the top.
  • Vahid Najafi
    Vahid Najafi about 5 years
    This doesn't work perfect when the item has a large height.
  • Noor Hossain
    Noor Hossain over 3 years
    @VahidNajafi so, what is the approach, When the list view larger as well as the items are large heights ?