开发者

Android ListView: overriding causes scrolling do not executed

开发者 https://www.devze.com 2023-02-15 20:34 出处:网络
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.

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 SimpleOnGestureLi开发者_Python百科stener() {
            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


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消