问题
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