Android ListView: overriding causes scrolling do not executed

微笑、不失礼 提交于 2019-12-07 22:53:30

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/5261217/android-listview-overriding-causes-scrolling-do-not-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!