Detect Scroll Up & Scroll down in ListView

2019-01-03 08:27发布

I have the following requirement:

  • At first, data for page no: 2 is fetched from the server & the items are populated in a ListView.

Considering that both the prev page & next page are available in a scenario, the following code has been added:

 if(prevPageNo > 0){
    mListViewActual.setOnScrollListener(this);
 }

 if(nextPageNo > 0){
    mListViewActual.setOnScrollListener(this);
 }

What conditions should I put to detect scroll up & scroll down on the following methods:

  1. void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
  2. void onScrollStateChanged(AbsListView view, int scrollState)

After the action: scroll up & scroll down is detected , accordingly a service will be called with either the prev page no or next page no , to fetch the items to be populated in the Listview.

Any inputs will be helpful.

Gone through the following links but its not returning the correct scroll up / scroll down action:

link 1 link 2

16条回答
beautiful°
2楼-- · 2019-01-03 09:15

try using the setOnScrollListener and implement the onScrollStateChanged with scrollState

setOnScrollListener(new OnScrollListener(){
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
      // TODO Auto-generated method stub
    }
    public void onScrollStateChanged(AbsListView view, int scrollState) {
      // TODO Auto-generated method stub
      final ListView lw = getListView();

       if(scrollState == 0) 
      Log.i("a", "scrolling stopped...");


        if (view.getId() == lw.getId()) {
        final int currentFirstVisibleItem = lw.getFirstVisiblePosition();
         if (currentFirstVisibleItem > mLastFirstVisibleItem) {
            mIsScrollingUp = false;
            Log.i("a", "scrolling down...");
        } else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
            mIsScrollingUp = true;
            Log.i("a", "scrolling up...");
        }

        mLastFirstVisibleItem = currentFirstVisibleItem;
    } 
    }
  });
查看更多
劳资没心,怎么记你
3楼-- · 2019-01-03 09:16

For some reason the Android doc doesnt cover this, and the method used isnt even in the docs... took me a while to find it.

To detect if your scroll is at the top you would use this.

public boolean checkAtTop() 
{
    if(listView.getChildCount() == 0) return true;
    return listView.getChildAt(0).getTop() == 0;
}

This will check if your scroller is at the top. Now, in order to do it for the bottom, you would have to pass it the number of children that you have, and check against that number. You might have to figure out how many are on the screen at one time, and subtract that from your number of children. I've never had to do that. Hope this helps

查看更多
乱世女痞
4楼-- · 2019-01-03 09:17

With all the method posted, there are problems recognizing when the user is scrolling up from the first element or down from the last. Here is another approach to detect scroll up/down:

        listView.setOnTouchListener(new View.OnTouchListener() {
            float height;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                float height = event.getY();
                if(action == MotionEvent.ACTION_DOWN){
                    this.height = height;
                }else if(action == MotionEvent.ACTION_UP){
                    if(this.height < height){
                        Log.v(TAG, "Scrolled up");
                    }else if(this.height > height){
                        Log.v(TAG, "Scrolled down");
                    }
                }
                return false;
            }
        });
查看更多
Summer. ? 凉城
5楼-- · 2019-01-03 09:18

Just set scroll listener to your listview.

If you have a header or footer you should check the visible count too. If it increases it means you are scrolling down. (Reverse it if there is a footer instead of header)

If you don't have any header or footer in your listview you can remove the lines which cheks the visible item count.

listView.setOnScrollListener(new AbsListView.OnScrollListener() {
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (mLastFirstVisibleItem > firstVisibleItem) {
                Log.e(getClass().toString(), "scrolling up");
            } else if (mLastFirstVisibleItem < firstVisibleItem) {
                Log.e(getClass().toString(), "scrolling down");
            } else if (mLastVisibleItemCount < visibleItemCount) {
                Log.e(getClass().toString(), "scrolling down");
            } else if (mLastVisibleItemCount > visibleItemCount) {
                Log.e(getClass().toString(), "scrolling up");
            }
            mLastFirstVisibleItem = firstVisibleItem;
            mLastVisibleItemCount = visibleItemCount;
        }

        public void onScrollStateChanged(AbsListView listView, int scrollState) {
        }
    });

and have this variables

int mLastFirstVisibleItem;
int mLastVisibleItemCount;
查看更多
登录 后发表回答