Skip to content

Instantly share code, notes, and snippets.

@derekbrameyer
Last active August 29, 2015 14:02
Show Gist options
  • Save derekbrameyer/a789f1977171f4040323 to your computer and use it in GitHub Desktop.
Save derekbrameyer/a789f1977171f4040323 to your computer and use it in GitHub Desktop.
InterceptingListView, an Android ListView to use with SlidingUpPanel
package com.doomonafireball.demo.android.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
/**
* User: derek Date: 12/7/13 Time: 2:37 PM
*
* This ListView intercepts all gestures when enabled, except for events when it has already been scrolled to the
* top.
*
* It is designed to work with the SlidingUpPanel.
*/
public class InterceptingListView extends ListView {
private boolean mIsIntercepting = false;
private float mCurrX = 0.0f;
private float mCurrY = 0.0f;
public InterceptingListView(Context context) {
super(context);
}
public InterceptingListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public InterceptingListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
super.onInterceptTouchEvent(event);
getParent().requestDisallowInterceptTouchEvent(mIsIntercepting);
final int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
mCurrX = event.getX();
mCurrY = event.getY();
}
return super.onInterceptTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getAction();
if (action == MotionEvent.ACTION_MOVE) {
int index = getFirstVisiblePosition();
View v = getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
if ((event.getY() - mCurrY) > Math.abs(event.getX() - mCurrX) && (index == 0) && (top
>= 0) && mIsIntercepting) {
// User scrolled vertically
mIsIntercepting = false;
getParent().requestDisallowInterceptTouchEvent(false);
}
}
return super.onTouchEvent(event);
}
public void setIsIntercepting(boolean isIntercepting) {
mIsIntercepting = isIntercepting;
getParent().requestDisallowInterceptTouchEvent(isIntercepting);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment