1

4つのクラスとxmlレイアウトを持つ4つのアクティビティがあります。4つのアクティビティでスワイプしたい!! 別の投稿からこのコードを使用します (この投稿:グリッド レイアウトでの Fling ジェスチャ検出)

新しいアクティビティを開いて leftToright または rightToleft スワイプをタッチするときに必要です。

私のコードはどこが間違っていますか:

私のコード:

package com.package110.Y;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log; 
import android.view.MotionEvent;
import android.view.View;

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

 // how to set this 3 line to in my code Or change or update it        

 //ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this);
 //lowestLayout = (RelativeLayout)this.findViewById(R.id.lowestLayout);
 //lowestLayout.setOnTouchListener(activitySwipeDetector);

   }




    public class ActivitySwipeDetector implements View.OnTouchListener {


            Intent left = new Intent(getApplicationContext(), left.class);
            Intent right = new Intent(getApplicationContext(), right.class);
            Intent top = new Intent(getApplicationContext(), top.class);
            Intent bottom = new Intent(getApplicationContext(), bottom.class);


        static final String logTag = "ActivitySwipeDetector";
        private Activity activity;
        static final int MIN_DISTANCE = 100;
        private float downX, downY, upX, upY;

        public ActivitySwipeDetector(Activity activity){
            this.activity = activity;
        }

        public void onRightToLeftSwipe(){
            Log.i(logTag, "RightToLeftSwipe!");
            activity.startActivity(right);

        }

        public void onLeftToRightSwipe(){
            Log.i(logTag, "LeftToRightSwipe!");
            activity.startActivity(left);
        }

        public void onTopToBottomSwipe(){
            Log.i(logTag, "onTopToBottomSwipe!");
            activity.startActivity(top);
        }

        public void onBottomToTopSwipe(){
            Log.i(logTag, "onBottomToTopSwipe!");
            activity.startActivity(bottom);
        }

        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()){
                case MotionEvent.ACTION_DOWN: {
                    downX = event.getX();
                    downY = event.getY();
                    return true;
                }
                case MotionEvent.ACTION_UP: {
                    upX = event.getX();
                    upY = event.getY();

                    float deltaX = downX - upX;
                    float deltaY = downY - upY;

                    // swipe horizontal?
                    if(Math.abs(deltaX) > MIN_DISTANCE){
                        // left or right
                        if(deltaX < 0) { this.onLeftToRightSwipe(); return true; }
                        if(deltaX > 0) { this.onRightToLeftSwipe(); return true; }
                    }
                    else {
                            Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                            return false; // We don't consume the event
                    }

                    // swipe vertical?
                    if(Math.abs(deltaY) > MIN_DISTANCE){
                        // top or down
                        if(deltaY < 0) { this.onTopToBottomSwipe(); return true; }
                        if(deltaY > 0) { this.onBottomToTopSwipe(); return true; }
                    }
                    else {
                            Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                            return false; // We don't consume the event
                    }

                    return true;
                }
            }
            return false;
        }

        }



  }

説明: 私のアプリは強制終了です !!! 私のGoogle APIはAndroid 3.0用です。私のアプリに問題がありますか?また、このコードを試してみると、強制的に閉じられます!!!!

別のコード:

package ir.package110.Y;


 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.GestureDetector;
 import android.view.GestureDetector.SimpleOnGestureListener;
 import android.view.MotionEvent;
 import android.view.View;

 public class ActivitySwipeDetectorActivity extends Activity {
          private GestureDetector gestureDetector;

          @Override
          public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);


              // ...

            gestureDetector = new GestureDetector(new SwipeGestureDetector());
          }

          /* ... */

            Intent right = new Intent(ActivitySwipeDetectorActivity.this.getBaseContext(), right.class);
            Intent left = new Intent(ActivitySwipeDetectorActivity.this.getBaseContext(), left.class);



          private void onLeftSwipe() {
            // Do something
            startActivity(left);

          }

          private void onRightSwipe() {
            startActivity(right);

              // Do something
          }

          // Private class for gestures
          private class SwipeGestureDetector extends SimpleOnGestureListener {
            // Swipe properties, you can change it to make the swipe
            // longer or shorter and speed
            private static final int SWIPE_MIN_DISTANCE = 120;
            private static final int SWIPE_MAX_OFF_PATH = 200;
            private static final int SWIPE_THRESHOLD_VELOCITY = 200;

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2,
                                 float velocityX, float velocityY) {
              try {
                float diffAbs = Math.abs(e1.getY() - e2.getY());
                float diff = e1.getX() - e2.getX();

                if (diffAbs > SWIPE_MAX_OFF_PATH)
                  return false;

                // Left swipe
                if (diff > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    ActivitySwipeDetectorActivity.this.onLeftSwipe();

                // Right swipe
                } else if (-diff > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    ActivitySwipeDetectorActivity.this.onRightSwipe();
                }
              } catch (Exception e) {
                Log.e("YourActivity", "Error on gestures");
              }
              return false;
            }
          }
        }          
4

2 に答える 2

1

これを行う最善の方法は、ViewPager を使用して、1 つのアクティビティと 4 つのフラグメントを使用することだと思います。

フラグメントに関するこのリンクを確認できます: http://androidcookbook.com/Recipe.seam;jsessionid=A2DD7A11C804B7C7646DCA883AA452FC?recipeId=1160

ViewPager に関するこの 1 つ: http://developer.android.com/reference/android/support/v4/view/ViewPager.html

于 2012-08-24T12:28:14.780 に答える