viewFlipper与手势

时间:2012-03-17 09:05:15

标签: android textview viewflipper gesture

我正在使用手势处理viewFlipper。但手势无法移动到下一页。

我设置了下一个和之前的手势,例如<,>通过设置手势来移动下一页。

我不知道我的代码有什么问题。

AndroidViewFlipper

public class AndroidViewFlipperActivity extends Activity {

ViewFlipper page;
GestureLibrary mLibrary;
GestureOverlayView gestures;

AnimationSet animSetFlipInForeward;
AnimationSet animSetFlipOutForeward;
AnimationSet animSetFlipInBackward;
AnimationSet animSetFlipOutBackward;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    page = (ViewFlipper)findViewById(R.id.flipper);

    gestures = (GestureOverlayView) findViewById(R.id.gestures);
    gestures.addOnGesturePerformedListener(mListener);
    mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);



}

OnGesturePerformedListener mListener = new OnGesturePerformedListener() {
public void onGesturePerformed(GestureOverlayView overlay,Gesture gesture) {
        ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
        if (predictions.size() != 0) {
            Prediction prediction = predictions.get(0);
            String name = prediction.name;
            if (prediction.score > 1.0) {
                if(name.equals("prev")){
                    Log.i("tag","next1");
                    SwipeRight();
                }else if(name.equals("next")){
                    Log.e("tag","next2");
                    SwipeLeft();
                }
            }
        }
    }
};


private void SwipeRight(){
    page.setInAnimation(animSetFlipInBackward);
    page.setOutAnimation(animSetFlipOutBackward);
    page.showPrevious();
}

private void SwipeLeft(){
    page.setInAnimation(animSetFlipInForeward);
    page.setOutAnimation(animSetFlipOutForeward);
    page.showNext();
}
}   

main.xml中      

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<ViewFlipper
    android:id="@+id/flipper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="@string/comment1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="@string/comment2"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="@string/comment3"/>
    </LinearLayout>

  </ViewFlipper>
</LinearLayout>
</android.gesture.GestureOverlayView>

1 个答案:

答案 0 :(得分:0)

这样做:

OnGesturePerformedListener mListener = new OnGesturePerformedListener() {
public void onGesturePerformed(GestureOverlayView overlay,Gesture gesture) {
        ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
        for (Prediction prediction : predictions) {
        if (predictions.size() != 0) {
            String name = prediction.name;
            if (prediction.score > 1.0) {
                if(name.equals("prev")){
                    Log.i("tag","next1");
                    SwipeRight();
                }else if(name.equals("next")){
                    Log.e("tag","next2");
                    SwipeLeft();
                }
            }
        }
        }
    }
};