Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HeaderUntouchableListView
/**
* listView with header untouchable
* Created by ninja on 8/3/16.
*/
public class HeaderUntouchableListView extends ListView {
private View mHeaderView;
private boolean isDownEventConsumed;
public HeaderUntouchableListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void addHeaderView(View v) {
super.addHeaderView(v);
this.mHeaderView = v;
}
@Override
public void addHeaderView(View v, Object data, boolean isSelectable) {
super.addHeaderView(v, data, isSelectable);
this.mHeaderView = v;
}
/**
* list header should not consume the event, and list item should consume the event
* consumed here is replaced with super.dispatchTouchEvent(motionEvent)
* @param motionEvent
* @return is event consumed
*/
@Override
public boolean dispatchTouchEvent(MotionEvent motionEvent) {
if(mHeaderView == null) return super.dispatchTouchEvent(motionEvent);
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
//if touch header not to consume the event
Rect rect = new Rect((int) mHeaderView.getX(), (int) mHeaderView.getY(), mHeaderView.getRight(), mHeaderView.getBottom());
if(rect.contains((int)motionEvent.getX(), (int)motionEvent.getY())){
isDownEventConsumed = false;
return isDownEventConsumed;
}else {
isDownEventConsumed = true;
return super.dispatchTouchEvent(motionEvent);
}
}else{
//if touch event not consumed, then move/up event should be the same
if(!isDownEventConsumed)return isDownEventConsumed;
return super.dispatchTouchEvent(motionEvent);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment