Question

Is there a way to get the direction of a Fling (up or down) in a ScrollListener attached to a ListView? I get the OnScrollListener.SCROLL_STATE_FLING without problems, but I can't get the direction. Do I have to write a whole GestureListener and onTouch listener and attach it to my ListView for this or is there an easier way? I just need the direction of the Fling nothing more. Thanks for your answers.

Was it helpful?

Solution

Do I have to write a whole GestureListener and onTouch listener and attach it to my ListView for this or is there an easier way?

You can simply use an OnTouchListener like this:

listView.setOnTouchListener(new OnTouchListener() {
    int before = -1;
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            before = ((ListView) v).getFirstVisiblePosition();
            break;
        case MotionEvent.ACTION_UP:
            int now = ((ListView) v).getFirstVisiblePosition();
            if(now < before)
                Log.v("onTouch", "Scroll Up");
            else if(now > before)
                Log.v("onTouch", "Scroll Down");

        }
        return false;
    }
});

If you only want to be notified when a fling happens: save the current scroll state in OnScrollListener's onScroll() method and check if is a fling motion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top