Given a parent View which contains other child Views, the way touch generally works in Android is that the parent View will only see a touch event if it's not consumed by one of its child Views, i.e. the child Views have first say on whether they want to act on a touch event. This is true in most circumstances but there are some exceptions. Notably, it is possible for a parent View to intercept touch events so that it processes them instead of the child Views. An example of this is the
ListView
class: when the user moves his/her finger up or down in the
ListView
, the touch events are intercepted by the
ListView
so that it scrolls vertically and the child Views do not see the touch events. If you'd like to stop the
ListView
stealing the touch events, here's how: instantiate the custom
ListView
class below and set
scrollEnabled
to
false
:
public class DisableScrollListView extends ListView {
/**
* Flag which determines whether vertical scrolling is enabled in this {@link ListView}.
*/
private boolean scrollEnabled = true;
public DisableScrollListView(Context context) {
super(context);
}
public DisableScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DisableScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (!scrollEnabled) {
return false;
} else {
return super.onInterceptTouchEvent(ev);
}
}
/**
* Sets the value of {@link #scrollEnabled}.
*
* @param scrollEnabled
*/
public void setScrollEnabled(boolean scrollEnabled) {
this.scrollEnabled = scrollEnabled;
}
}
No comments:
Post a Comment