Detecting the scrolling direction in the adapter (up/down)

后端 未结 11 1585
臣服心动
臣服心动 2020-12-01 00:38

I am trying to mimic the Google Plus application in my project, as it seems to be the reference now.

The listview effect when scrolling is really nice and I would li

11条回答
  •  情深已故
    2020-12-01 01:09

    Assign an OnScrollListener to your ListView. Create a flag which indicates whether the user is scrolling up or down. Set an appropriate value to the flag by checking if the current first visible item position equals to more or less than the previous first visible item position. Put that check inside onScrollStateChanged().

    Sample code:

    private int mLastFirstVisibleItem;
    private boolean mIsScrollingUp;
    
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        final ListView lw = getListView();
    
        if (view.getId() == lw.getId()) {
            final int currentFirstVisibleItem = lw.getFirstVisiblePosition();
    
            if (currentFirstVisibleItem > mLastFirstVisibleItem) {
                mIsScrollingUp = false;
            } else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
                mIsScrollingUp = true;
            }
    
            mLastFirstVisibleItem = currentFirstVisibleItem;
        } 
    }
    

    Check if mIsScrollingUp is true or false in getView(), and assign the animations accordingly.

提交回复
热议问题