如何处理左侧滑动和右侧滑动事件

时间:2012-11-05 12:04:21

标签: android events

我想知道用户在手机上向左或向右滑动时需要处理的事件。

对于Ex:在电话日志或联系人中,当用户在A名称上向左滑动时,它会开始呼叫该联系人,并且当幻灯片右侧消息应用程序打开时。

我想知道如何做到这一点,一个小代码片段会更好。

由于

4 个答案:

答案 0 :(得分:5)

您可以使用此监听器并将代码添加到onSwipeLeft()onSwipeRight()

public class OnSwipeTouchListener implements OnTouchListener {

@SuppressWarnings("deprecation")
private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());

public boolean onTouch(final View v, final MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        onTouch(e);
        return true;
    }


    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
            } else {
               // onTouch(e);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}


public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeTop() {
}

public void onSwipeBottom() {
}
}

答案 1 :(得分:5)

OnSwipeTouchListener.java:

import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class OnSwipeTouchListener implements OnTouchListener {

    private final GestureDetector gestureDetector;
public OnSwipeTouchListener (Context ctx){
    gestureDetector = new GestureDetector(ctx, new GestureListener());
}

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
                result = true;
            } 
            else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                }
                result = true;

        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeTop() {
}

public void onSwipeBottom() {
}
}

用法:

imageView.setOnTouchListener(new OnSwipeTouchListener() {
    public void onSwipeTop() {
        Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show();
    }
    public void onSwipeRight() {
        Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show();
    }
    public void onSwipeLeft() {
        Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
    }
    public void onSwipeBottom() {
        Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show();
    }

public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}
});

答案 2 :(得分:0)

看看那个。我认为你需要手势识别。

@Vogella解释得非常好。

http://www.vogella.com/articles/AndroidGestures/article.html

答案 3 :(得分:-1)

你需要使用GestureDetector和SimpleOnGestureListener。这是在特定视图上使用滑动的代码。这是FrameLayout的一个:

package com.ex.gessture;

import android.app.Activity;
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;
import android.widget.FrameLayout;

public class GestureListenerActivity extends Activity {

FrameLayout frame;
GestureDetector mGestureDetector;
String TAG = "GestureListenerActivity";

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

    frame = (FrameLayout) findViewById(R.id.frame);

    mGestureDetector = new GestureDetector(this, mGestureListener);

    frame.setOnTouchListener(new FrameLayout.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            return mGestureDetector.onTouchEvent(event);
        }
    });
}

    /**
 * Gesture Event Handler
 */
private SimpleOnGestureListener mGestureListener = new SimpleOnGestureListener() {

    int swipe_Min_Distance = 100;
    int swipe_Max_Distance = 350;
    int swipe_Min_Velocity = 100;
/*      
    @Override
    public boolean onDown(MotionEvent e) {
        Log.i(TAG, "[CALLBACK_GL] boolean onDown(e:" + e + ")");
        return super.onDown(e);
    }
*/
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e)
    {

        /**
         *
         * Do your stuff
         *
         */

        return super.onSingleTapUp(e);
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        Log.i(TAG, "[CALLBACK_GL] boolean onSingleTapConfirmed(e:" + e + ")");
        return super.onSingleTapConfirmed(e);
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        Log.i(TAG, "[CALLBACK_GL] boolean onDoubleTap(e:" + e + ")");


        return super.onDoubleTap(e);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Log.i(TAG, "[CALLBACK_GL] boolean onFling(e1:" + e1 + ", e2:" + e2 + ", velocityX:" + velocityX
                + ", velocityY:" + velocityY + "");

        final float xDistance = Math.abs(e1.getX() - e2.getX());
        final float yDistance = Math.abs(e1.getY() - e2.getY());

        if(xDistance > this.swipe_Max_Distance || yDistance > this.swipe_Max_Distance)
            return false;

        velocityX = Math.abs(velocityX);
        velocityY = Math.abs(velocityY);
        boolean result = false;

        if(velocityX > this.swipe_Min_Velocity && xDistance > this.swipe_Min_Distance){
            if(e1.getX() > e2.getX()) // right to left
                //this.listener.onSwipe(SWIPE_LEFT);
                Log.i(TAG, "Swipe Left");

            else
                Log.i(TAG, "Swipe Right");
                //this.listener.onSwipe(SWIPE_RIGHT);
        }   

        //return super.onFling(e1, e2, velocityX, velocityY);
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        Log.i(TAG, "[CALLBACK_GL] void onShowPress(e:" + e + ")");
        super.onShowPress(e);
    }

    @Override
    public void onLongPress(MotionEvent e) {
        Log.i(TAG, "[CALLBACK_GL] void onLongPress(e:" + e + ")");

        super.onLongPress(e);
    }
};

}    

main.xml中:

<?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" android:weightSum="1">

<FrameLayout
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_weight=".5"
    android:background="#ffffff"
    android:layout_margin="50dp">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" android:layout_gravity="center"/>

</FrameLayout>

这是使用单独的类并在整个屏幕上使用运动事件的不同方法的链接,而不仅仅是特定的视图:http://misha.beshkin.lv/android-swipe-gesture-implementation/。此链接将解决您的所有问题!