My first surprise was that it was harder than I expected! I shouldn't have been surprised :) Seriously, I thought that I could just wrap my existing view with the ViewFlipper and provide an index for 'paging'. I ended up implementing (hard-coding) 3 static pages with the intention of just ignoring the swipe if there weren't any next / previous screens. My second surprise was that I couldn't get the onFling / GestureDetector to fire. I thought I had grown a solid understanding of Android but sheesh - this should be trivial stuff.
Added to onCreate
mViewFlipper = (ViewFlipper) findViewById(R.id.main_flipper);
mGestureDetector = new GestureDetector(new FlingGestureDetector());
mGestureListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.d(getClass().getSimpleName(), "HERE onTouch");
if (mGestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
Then I added this inner class. Just a quick reminder, this doesn't currently work - I'm just recording the notes here.
class FlingGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent eventStart, MotionEvent eventEnd, float velocity, float unusedVelocity) {
Log.i(getClass().getSimpleName(), "onFling (" + eventStart + ", " + eventEnd + ", " + velocity + ")");
try {
if (Math.abs(eventStart.getY() - eventEnd.getY()) > FLING_MAX_DEVIATION) {
Log.d(MainActivity.class.getSimpleName(), "onFling too much Y in motion, not fling");
return false;
}
if (eventStart.getX() - eventEnd.getX() > FLING_MIN_DISTANCE && Math.abs(velocity) > FLING_MIN_VELOCITY) {
mViewFlipper.setInAnimation(AnimationUtils.makeInAnimation(MainActivity.this, true));
mViewFlipper.setOutAnimation(AnimationUtils.makeOutAnimation(MainActivity.this, true));
mViewFlipper.showNext();
} else if (eventEnd.getX() - eventStart.getX() > FLING_MIN_DISTANCE && Math.abs(velocity) > FLING_MIN_VELOCITY) {
mViewFlipper.setInAnimation(AnimationUtils.makeInAnimation(MainActivity.this, false));
mViewFlipper.setOutAnimation(AnimationUtils.makeOutAnimation(MainActivity.this, false));
mViewFlipper.showPrevious();
}
} catch (Exception e) {
Log.w(MainActivity.class.getSimpleName(), "onFling ignoring unexpected exception: " + e);
}
return false;
}
}
OnTouchListener mGestureListener;
/**
* Most people will probably just have the 1 page of buckets but for the
* people lucky enough to have more than 9 / 12 / whatever the number is,
* they can fling thru the pages.
*/
private int mViewIndex = 0;
Blah blah blah - just a distraction. I need to get back to fixing sync'ing and adding buckets.
No comments:
Post a Comment