2012-11-05 40 views
1

我想知道當用戶在手機上向左或向右滑動時需要處理的事件。如何處理左滑動和右滑動事件

例如:在用戶在名稱上滑動時在電話日誌或聯繫人中,它開始調用該聯繫人,並在幻燈片右側消息應用程序打開時。

我想知道如何做到這一點,一段代碼片段會更好。

謝謝

回答

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; 
    } 
} 

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    return false; 
} 
public void onSwipeRight() { 
} 

public void onSwipeLeft() { 
} 

public void onSwipeTop() { 
} 

public void onSwipeBottom() { 
} 
} 
+0

但我怎麼能使用它。請給我一個例子。例如,我不知道如何將它放在我的MainActivity中,以及如何將該事件放在名稱中,我需要處理此事件。 –

-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/。此鏈接將解決您的所有問題!

4

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); 
} 
}); 
+1

酷:)工作就像一個魅力:) –