In the end I've solved it by extending the ListView
class:
public class SingleScrollableHeaderListView extends ListView {
private View mHeaderView;
public SingleScrollableHeaderListView(Context context) {
super(context);
}
public SingleScrollableHeaderListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SingleScrollableHeaderListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (mHeaderView != null) return mHeaderView.onTouchEvent(ev);
return super.onInterceptTouchEvent(ev);
}
@Override
public void addHeaderView(View v, Object data, boolean isSelectable) {
if (mHeaderView != null) removeHeaderView(mHeaderView);
mHeaderView = v;
super.addHeaderView(v, data, isSelectable);
}
}
The significant part is the onInterceptTouchEvent()
method - here I'm passing the MotionEvent
instance to the header view if it exists, otherwise I let the ListView
to deal with the motion event.