【问题标题】:Swipe listeners in Android在 Android 中滑动监听器
【发布时间】:2011-05-12 09:29:35
【问题描述】:

Android 中有类似 onLeftSwipeListener 和 onRightSwipeListener 的东西吗?我想用手指前后滑动来切换视图。我在其中使用 FrameLayout 和 ImageView。

【问题讨论】:

    标签: android


    【解决方案1】:

    它被称为GestureDetector 和SimpleOnGestureListener,其中有onFling()。在这种情况下,它被称为投掷而不是滑动:)

    【讨论】:

    • 哦,我明白了。但是它可以区分左撇子和右撇子吗?
    • 您可以在这里找到详细信息:developer.android.com/reference/android/view/…, android.view.MotionEvent, float, float)
    • 还有一个问题。使用意图来切换视图是否就足够了,还是一种愚蠢的方式?
    【解决方案2】:
    yourView.setOnTouchListner(onThumbTouch );
    
        OnTouchListener onThumbTouch = new OnTouchListener() {
                float previouspoint = 0 ;
                float startPoint=0;
                @Override
                public boolean onTouch(View v, MotionEvent event) {   
                    switch(v.getId()) {
                        case R.id.tvDetailsalaujairiyat: // Give your R.id.sample ...
                            switch(event.getAction()){
                                case MotionEvent.ACTION_DOWN:
                                    startPoint=event.getX();
                                    System.out.println("Action down,..."+event.getX());
                                break;
                                case MotionEvent.ACTION_MOVE:
    
                                break;
                                case MotionEvent.ACTION_CANCEL:
                                    previouspoint=event.getX();
                                    if(previouspoint > startPoint){
                                       //Right side swipe        
                                    }else{
                                       // Left side swipe
                                    }        
                                break;
                           }
                        break;
                    }
                    return true;
                }
            };
    

    【讨论】:

    • 最佳答案...客观!
    【解决方案3】:

    我在android中做了一个swiper的小例子,我想和你分享代码。

    看看这个。

    //布局///

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <ViewFlipper
            android:id="@+id/view_flipper"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical" >
    
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Soy A"
                android:textColor="#FFFFFF" />
    
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Soy B"
                android:textColor="#BBBFFF" />
    
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Soy C"
                android:textColor="#FFBBFF" />
    
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Soy D"
                android:textColor="#FFFFAF" />
        </ViewFlipper>
    
    </LinearLayout>
    

    ///活动///

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.GestureDetector;
    import android.view.GestureDetector.SimpleOnGestureListener;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    import android.view.animation.AccelerateInterpolator;
    import android.view.animation.Animation;
    import android.view.animation.TranslateAnimation;
    import android.widget.ViewFlipper;
    
    public class SwipeActivity extends Activity {
    
        private Animation mInFromRight;
        private Animation mOutToLeft;
        private Animation mInFromLeft;
        private Animation mOutToRight;
        private ViewFlipper mViewFlipper;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            mViewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
            mViewFlipper.setDisplayedChild(0);
            initAnimations();
        }
    
        private void initAnimations() {
            mInFromRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,
                    +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f);
            mInFromRight.setDuration(500);
            AccelerateInterpolator accelerateInterpolator = new AccelerateInterpolator();
            mInFromRight.setInterpolator(accelerateInterpolator);
    
            mInFromLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,
                    -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f);
            mInFromLeft.setDuration(500);
            mInFromLeft.setInterpolator(accelerateInterpolator);
    
            mOutToRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,
                    0.0f, Animation.RELATIVE_TO_PARENT, +1.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f);
            mOutToRight.setDuration(500);
            mOutToRight.setInterpolator(accelerateInterpolator);
    
            mOutToLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, -1.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f);
            mOutToLeft.setDuration(500);
            mOutToLeft.setInterpolator(accelerateInterpolator);
    
            final GestureDetector gestureDetector;
            gestureDetector = new GestureDetector(new MyGestureDetector());
    
            mViewFlipper.setOnTouchListener(new OnTouchListener() {
    
                public boolean onTouch(View v, MotionEvent event) {
                    if (gestureDetector.onTouchEvent(event)) {
                        return false;
                    } else {
                        return true;
                    }
                }
            });
        }
    
        private class MyGestureDetector extends SimpleOnGestureListener {
    
            private static final int SWIPE_MIN_DISTANCE = 120;
            private static final int SWIPE_MAX_OFF_PATH = 250;
            private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                    float velocityY) {
                System.out.println(" in onFling() :: ");
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    mViewFlipper.setInAnimation(mInFromRight);
                    mViewFlipper.setOutAnimation(mOutToLeft);
                    mViewFlipper.showNext();
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    mViewFlipper.setInAnimation(mInFromLeft);
                    mViewFlipper.setOutAnimation(mOutToRight);
                    mViewFlipper.showPrevious();
                }
                return super.onFling(e1, e2, velocityX, velocityY);
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      这是一个关于实现手势检测的相关问题:

      Fling gesture detection on grid layout

      您还可以使用带有一些动画的 ViewFlipper 在视图之间切换,看起来不错,这是一个在 viewflipper 上启用左/右滑动的示例实现:

      http://www.inter-fuser.com/2009/07/android-transistions-slide-in-and-slide.html

      【讨论】:

      • 我可以在我的模拟器上进行滑动(滑动)吗?
      • 是的,应该没有问题
      • 我使用了 Gav 的代码 sn-p,但不管我是翻转还是点击,我总是会得到点击。我怎样才能同时拥有它们?如何区分它们?
      • 我尝试使用模拟器,只是为了确保它可以在上面运行,并且运行良好,也许你遗漏了什么,检查你的代码 aginst 来自 gav 的代码,它适用于我.当您需要检测滚动视图中的投掷时也存在一些问题,是您的情况吗?
      • 谢谢,现在可以了。我有一张照片。它占用了我所有的屏幕。当我从右向左滑动时,我必须更改图片。所以我称之为意图。但它不起作用。这是代码:public SwipeDetector(Activity activity, Class called_class){ this.activity = activity; this.called_class = called_class; } 然后我有public void onRightToLeftSwipe(){ Intent i = new Intent(activity, called_class); startActivity(i); } 并在我的活动中:SwipeDetector swipeDetector = new SwipeDetector(this, SecondScreen.class); myView.setOnTouchListener(activitySwipeDetector);
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多