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

后端 未结 11 1607
臣服心动
臣服心动 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:05

    General solution that doesn't rely on positions of views/etc. Just check the vertical scroll offset and compare it to the previous scroll offset. If the new value is greater than the old the user is scrolling down, and vice-versa.

    // [START check vertical scroll direction]
    int oldScrollOffset = 0;
    
    listView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View view, int i, int i1, int i2, int i3) {
    
                Boolean scrollDirectionDown;
    
                int newScrollOffset = listView.computeVerticalScrollOffset();
    
                if (newScrollOffset > oldScrollOffset) {
                    scrollDirectionDown = true;
                } else {
                    scrollDirectionDown = false;
                }
    
                oldScrollOffset = newScrollOffset;
    
                if (scrollDirectionDown) {
                    // Update accordingly for scrolling down
                    Log.d(TAG, "scrolling down");
                } else {
                    // Update accordingly for scrolling up
                    Log.d(TAG, "scrolling up");
                }
    
        });
    // [END check vertical scroll direction]
    

提交回复
热议问题