Last active
August 29, 2015 14:02
-
-
Save derekbrameyer/a789f1977171f4040323 to your computer and use it in GitHub Desktop.
InterceptingListView, an Android ListView to use with SlidingUpPanel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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