Giorgio Vespucci
Giorgio Vespucci

Reputation: 1586

Android ListView: overriding causes scrolling do not executed

I want to know if user is scrolling up or down. I have began to override the OnGestureListener.onScroll() method and set my GestureDetector for the ListView.

public class ContactList extends ListView {

    GestureDetector gestureDetector;

    public ContactList(Context context) {
        super(context);
        initView();
    }

    public ContactList(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    public ContactList(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        gestureDetector = new GestureDetector(new SimpleOnGestureListener() {
            public boolean onScroll(MotionEvent firstEvent, MotionEvent secondEvent, float distanceX, float distanceY) {
                Log.d("tag", "onScroll "+firstEvent.getAction());
                Log.d("tag", "onScroll "+secondEvent.getAction());
                return true;

            };
        });

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
}

Debugging I noticed that it passes from onTouchEvent() but not from onScroll() and the scroll is not perfomed anymore by the ListView.

What am I doing wrong?

Thank you

Upvotes: 2

Views: 3283

Answers (1)

Matthew
Matthew

Reputation: 44919

Have you tried calling super.onTouchEvent(event) in your onTouchEvent method?

Edit - I think you want to return false as well in your onScroll method.

Then your onTouchEvent method should be:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (gestureDetector.onTouchEvent(event)) {
        return true;
    }
    return super.onTouchEvent(event);
}

Upvotes: 4

Related Questions