我有一個ViewFlipper,它應該對一個手勢作出反應,但事實並非如此。Android 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;
}
查看與ViewFlipper它實現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;
}
}
佈局
<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);
}
我發現了這個錯誤。我沒有在課堂上的某個時刻保存上下文。上面的代碼工作完美。 – dbrettschneider 2011-04-06 11:19:45
從哪裏得到「IFlingable」?無法在Android API中看到它。另外,「源」是什麼類型的對象?還有,「GUIHelper」來自的idk。你在背景中做了一些事情,使這個例子有點難以遵循。 – moonlightcheese 2011-05-04 23:38:10
我自己做了。我編輯了帖子,並把它們放進去。 – dbrettschneider 2011-05-05 06:20:48