我有一个 ViewFlipper 应该对投掷手势做出反应,但它没有。
活动
@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;
}
使用实现 IFlingable 的 ViewFlipper 查看
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;
}
}
布局
<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>
日志
fling: x=2542.3613 y=95.877945
flip: 0
piep
我得到了正确的日志消息,因此 ViewFlipper 上的 showNext() 被执行,但它不会改变它在 gui 上的视图。我错过了什么吗?我有另一种使用 ViewSwitcher 而不是 Flipper 的布局,并且该布局有效。
编辑:
以下是缺少的课程:
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);
}