0

I need to check the direction of the scroll when my listview is scroll up or down, I am getting it as:

int lastVisibleItem = 0;
boolean isScrollingDown = false;

void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    if (firstVisibleItem > lastVisibleItem) {
        isScrollingDown = true;
        Log.e("logkey","down");
    }
    else {
        Log.e("logkey","up");
        isScrollingDown = false;
    }
    lastVisibleItem = firstVisibleItem;
}

The problem is when the visible items are equal to the screen or when there are items to the whole screen suppose that only 6 items fit in the screen and the last item is half visible, the log cat starts showing me the both down and up at the same time!

In simple words, in the above case, the scroll direction is ambiguous to get when there are items equal to the screen to fit in and the very last item is half visible and when I scroll I am getting this problem!

Can somebody please tell me what I am doing wrong? Thanks in advance!

MarGin
  • 2,078
  • 1
  • 17
  • 28
Java Nerd
  • 137
  • 1
  • 4
  • 17

2 Answers2

0

Implement the listview ScrollListener

         listview.setOnScrollListener(new OnScrollListener() {
                 private int LastVisibleItem;

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

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

                    if(LastVisibleItem<firstVisibleItem){
                        Log.d("Tag","Scroll down");
                      }
                    if(LastVisibleItem>firstVisibleItem){
                        Log.d("Tag","Scroll up");
                      }
                     LastVisibleItem=firstVisibleItem;
               }
            });
Zahoor Saleem
  • 614
  • 7
  • 15
0

So luckily i found something great on the GitHub! I have changed my simple native ListView to Observable List View listed here

   https://github.com/ksoichiro/Android-ObservableScrollView

and it worked like a charm, as i want to work it as!

Java Nerd
  • 137
  • 1
  • 4
  • 17