I have a ViewFlipper that should respond to fling gestures, but it is not.
Activities
@Override public void onCreate(Bundle savedInstanceState) { ... listView = this.getListView(); detector = new GestureDetector(this, new FlingGestureListener(listView)); ... }
FlingGestureListener
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { int pos = source.pointToPosition(Math.round(e1.getX()), Math.round(e1.getY())); View v = source.getChildAt(pos - source.getFirstVisiblePosition()); System.out.println("fling: x=" + velocityX + " y=" + velocityY); try { IFlingable flingable = (IFlingable) v; if(velocityY > -200 && velocityY < 200) { if(velocityX < 0) flingable.fling(IFlingable.RIGHT_TO_LEFT); else if(velocityX > 0) flingable.fling(IFlingable.LEFT_TO_RIGHT); } } catch(Exception e) {} return false; }
View with ViewFlipper that implements IFlingable
public void fling(int direction) { System.out.println("flip: " + direction); switch(direction) { case IFlingable.LEFT_TO_RIGHT: System.out.println("piep"); GUIHelper.setAnimationSlideLeftToRight(context, switcher); switcher.showNext(); break; case IFlingable.RIGHT_TO_LEFT: System.out.println("pup"); GUIHelper.setAnimationSlideRightToLeft(context, switcher); switcher.showPrevious(); break; } }
Markup
<ViewFlipper android:id="@+id/viewSwitcher" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_weight="1" android:inAnimation="@anim/slide_in_left" android:outAnimation="@anim/slide_out_right"> <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical"> ... </LinearLayout> ... </ViewFlipper>
Magazine
fling: x=2542.3613 y=95.877945 flip: 0 piep
I get the correct log messages, so showNext () on the ViewFlipper is executed, but it does not change its view to gui. Am I missing something? I have a different layout with ViewSwitcher instead of Flipper and it works.
EDIT:
Here are the missing classes:
public class GUIHelper { ... public static void setAnimationSlideLeftToRight(Context context, ViewAnimator switcher) { Animation in = AnimationUtils.loadAnimation(context, R.anim.slide_in_left); Animation out = AnimationUtils.loadAnimation(context, R.anim.slide_out_right); switcher.setInAnimation(in); switcher.setOutAnimation(out); } public static void setAnimationSlideRightToLeft(Context context, ViewAnimator switcher) { Animation in = AnimationUtils.loadAnimation(context, R.anim.slide_in_right); Animation out = AnimationUtils.loadAnimation(context, R.anim.slide_out_left); switcher.setInAnimation(in); switcher.setOutAnimation(out); } ... } public interface IFlingable { public static final int LEFT_TO_RIGHT = 0; public static final int RIGHT_TO_LEFT = 1; public void fling(int direction, boolean fling); }