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

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

    The accepted answer doesn't really "detect" scrolling up or down. It won't work if the current visible item is really huge. Using onTouchListener is the way to go.

    This is the code snippet I used:

    listView.setOnTouchListener(new View.OnTouchListener() {
        float initialY, finalY;
        boolean isScrollingUp;
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = MotionEventCompat.getActionMasked(event);
    
            switch(action) {
                case (MotionEvent.ACTION_DOWN):
                    initialY = event.getY();
                case (MotionEvent.ACTION_UP):
                    finalY = event.getY();
    
                    if (initialY < finalY) {
                        Log.d(TAG, "Scrolling up");
                        isScrollingUp = true;
                    } else if (initialY > finalY) {
                        Log.d(TAG, "Scrolling down");
                        isScrollingUp = false;
                    }
                default:
            }
    
            if (isScrollingUp) {
                // do animation for scrolling up
            } else {
                // do animation for scrolling down
            }
    
            return false; // has to be false, or it will freeze the listView
        }
    });
    

提交回复
热议问题