简体   繁体   中英

Android : Detect ListView scroll up and down listener

In my application trying to listen the listView scroll movement whether it is up or down.

contactsList.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

            view.setOnTouchListener(new View.OnTouchListener() {

                private float mInitialX;
                private float mInitialY;
                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    switch (event.getAction())
                    {
                        case MotionEvent.ACTION_DOWN:
                            mInitialX = event.getX();
                            mInitialY = event.getY();
                            return true;
                        case MotionEvent.ACTION_MOVE:
                            final float x = event.getX();
                            final float y = event.getY();
                            final float yDiff = y - mInitialY;
                            if (yDiff > 0.0) {
                                Log.e("Tag ","Scroll down");

                                break;

                            } else if (yDiff < 0.0) {
                                Log.e("Tag ","Scroll up");

                                break;

                            }
                            break;
                    }
                    return false;
                }
            });
        }

I've tried this code. But it is not working perfectly.

When I'm scrolling up , it shows in the log "Scroll Up" at first time, then it shows "Scroll Down".

Can anyone tell me why it behaves like this.

Thanks in advance :)

尝试此解决方案 ,您的问题似乎很相似,也许跟踪滚动方向足以满足您的需求。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM