3

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

例如:在电话日志或联系人中,当用户在 A name 上向左滑动时,它开始呼叫该联系人,并且当向右滑动消息应用程序时打开。

我想知道怎么做,一点代码片段会更好。

谢谢

4

4 回答 4

5

您可以使用此侦听器并将您的代码添加到onSwipeLeft()and 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() {
}
}
于 2012-11-05T12:29:50.673 回答
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);
}
});
于 2015-04-08T04:53:41.517 回答
0

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

@Vogella 解释得很好。

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

于 2012-11-05T12:26:01.957 回答
-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);
    }
};

}    

主.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/。这个链接将解决你所有的问题!

于 2012-11-05T12:31:46.837 回答