2012-11-05 2 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

는 U는 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

cool :) 매력처럼 작동합니다 :) –

관련 문제