So to begin with if you know of a better way of doing this…. PLEASE TELL ME… I spent a day or so on this trying to find more elegant solutions and in the end this seems like it will have to do.
First the problem. Android doesn’t have by default the swipie Scroll Content controller that iOS has examples of (I should clarify that iOS doesn’t have this by default but at least has examples.) So essentially I wanted something that would act like a gallery view (although just swipe one item onto the screen at a time) and then have views with buttons and what not that would be clickable. The problem being that if you have a clickable buttom and you start the swipe by touching it, it doesn’t swype… and if you set the ontouchlistener to the button to a custom one well say goodbye to the onclick… exmple–
———————— g is a galleryview btw
OnTouchListener mOnTouch = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (g.onTouchEvent(event)) {
Log.e(“onTouch”, “captured by gallery”);
return false;
}
Log.e(“onTouch”, “this has been touched!”);
return false;
}
};
button.setOnTouchListener(mTouch);
—————————
In this case for some really weird reason checking the if (g.onTouchEvent(event)) actually eats the event and even return false afterwards much to my dismay doesn’t even pass the event on!!!! this is what really kills me and if someone knows why… please oh please tell me.
Solutions-> Create a custom Gallery sample bellow… which overides the onInterceptTouchEvent with a custom Gesturedetector in an inner class that then calls the Galleries OnFling if there is a fling. The nice added part to this is that now you don’t need to set an ontouch listerner for each clickable item in the gallery.
couple other notes -> invalie return for position is so that it doesn’t try to click the item no matter where you are… It may not be needed anymore but I’m tired of messing with it for the time being… I’ll update if it can go by the way side. The velocityx / 4 is so that only one item gets flung
and the * 3 later on I have no idea why it’s needed
seems like the galleries ontouchevent does some black magic before sending the values to the onfling. 3 seems to be the majic number to make it work like the /4
public class MyGallery extends Gallery {
GestureDetector gestureDetector = new GestureDetector(new innerGestureDetector());
public MyGallery(Context context,AttributeSet attrSet) {
super(context, attrSet);
// TODO Auto-generated constructor stub
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.e(“FlingGallery”, “Galery was flinged”);
super.onFling(e1, e2, velocityX / 4, velocityY);
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (gestureDetector.onTouchEvent(ev))
{
Log.e(“FlingGallery”, “WT1″);
super.onTouchEvent(ev);
return true;
}
Log.e(“hijack”, “intercepted”);
return super.onInterceptTouchEvent(ev);
}
@Override
public int pointToPosition(int x, int y)
{
return INVALID_POSITION;
}
class innerGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.e(“fling”, “innerfling”);
return MyGallery.this.onFling(e1,e2,velocityX * 2,velocityY);
}
}
}
Hopefully someone will find this of use or at least it will help me with my memory… This was enough to make me hate how events are passed around (perhaps just because I don’t fully understand them)
Cheers,