Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A slightly customized list view for displaying shows. Provides handling for touch event greed. This is used so that the main list will not deprive the ViewPager List from horizontal swipe events for changing displayed shows per channel.
package ph.com.voyager.epg.ui;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;
/**
* A slightly customized list view for displaying shows. Provides handling for touch event greed.
* This is used so that the main list will not deprive the ViewPager List from horizontal swipe
* events for changing displayed shows per channel.
*
* See {@link GuideChannelFragment} (viewpagers) and {@link GuideFragment} (parent fragment)
*
* @author jerieljan
*/
public class ShowListView
extends ListView {
private float xDistance, yDistance, lastX, lastY;
private boolean interceptFlag = false;
public ShowListView(Context context) {
super(context);
}
public ShowListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Returns the previous state of the list view's touch event.
*
* @return true when the previous interaction was intercepted (manually triggered).
*/
public boolean wasIntercepted() {
if (!interceptFlag) {
interceptFlag = true;
return false;
}
return interceptFlag;
}
/**
* Default onInterceptTouchEvent, with better handling on which view gets touch focus more. This
* code block checks if the Y axis was moved more than the X axis.
*
* @return true if the listview takes touch focus. else, false.
*/
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
//When the user starts pressing, record.
xDistance = yDistance = 0f;
lastX = ev.getX();
lastY = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
//While in motion, get the X and Y
final float curX = ev.getX();
final float curY = ev.getY();
//Consider it with the previous values
xDistance += Math.abs(curX - lastX);
yDistance += Math.abs(curY - lastY);
lastX = curX;
lastY = curY;
//Then check if the user is scrolling vertically or horizontally. Intercept if vertical.
if (xDistance > yDistance) {
return interceptFlag = false;
}
}
return interceptFlag = super.onInterceptTouchEvent(ev);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment