android - 如何在 Activity 之间滑动(我的代码哪里错了???)

标签 android android-viewpager swipe

我有 4 个 Activity ,有 4 个类和 xml 布局;我想在 4 个 Activity 之间滑动!! 我使用另一篇文章中的这段代码(这篇文章:Fling gesture detection on grid layout)

我想在打开新 Activity 时触摸 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;
        }

        }



  }

解释: 我的应用程序强制关闭!!! 我的谷歌 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;
            }
          }
        }          

最佳答案

我认为最好的方法是一个 Activity 和 4 个 fragment ,以及一个 ViewPager。

您可以查看有关 fragment 的链接: http://androidcookbook.com/Recipe.seam;jsessionid=A2DD7A11C804B7C7646DCA883AA452FC?recipeId=1160

这是关于 ViewPager 的: http://developer.android.com/reference/android/support/v4/view/ViewPager.html

关于android - 如何在 Activity 之间滑动(我的代码哪里错了???),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12109307/

相关文章:

android - PagerAdapter 起始位置

没有 TLS 的 Android P : network-security-config: cleartextTrafficPermitted not possible for IP (only domain)

java - 如何在 App Resume 或 Re-load Object 中通过 Activity 实例化 Fragment?

Android 微调器和更改文本取决于项目选择

android - 如何将ViewPager放在listview之上?

Android - Fragments 和 Tabs 通信

android swipelist 缺少属性

java - 如何在长按主页、后退或最近使用的应用程序按钮时打开我的 Android 应用程序?

android - ViewPager fragment 都引用相同的 RecyclerView 和/或适配器

ios - 用于 View Controller 之间交互式滑动转换的简单库