2013-02-23 98 views
2

我想在我的首選項屏幕中添加手勢,但我沒有收到任何積極的結果,我的問題是我們可以添加手勢來選擇首選項屏幕,如果是,那麼我如何使用下面的 代碼中加入的手勢優先(不工作的偏好,但其做兩份活動之間切換)如何將手勢添加到首選項屏幕

@Override 
    public boolean onTouchEvent(MotionEvent event) { 
     // TODO Auto-generated method stub 

     return gestureDetector.onTouchEvent(event); 
    } 

    SimpleOnGestureListener simpleOnGestureListener 
    = new SimpleOnGestureListener(){ 


     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
       float velocityY) { 
      String swipe = ""; 
      float sensitvity = 50; 

      // TODO Auto-generated method stub 
      if((e1.getX() - e2.getX()) > sensitvity){ 
       // for left 
      Intent i=new Intent(getApplicationContext(),MyActivity.class); 
       startActivity(i); 
       finish(); 


      }else if((e2.getX() - e1.getX()) > sensitvity){ 
       //for right 
      }else{ 
       swipe += "\n"; 
      } 

      if((e1.getY() - e2.getY()) > sensitvity){ 
       //Swipe Up 
      }else if((e2.getY() - e1.getY()) > sensitvity){ 
       //Swipe Down 
      } 



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

    GestureDetector gestureDetector 
    = new GestureDetector(simpleOnGestureListener); 

回答

0

先爲您的MainActivity implements OnGestureListener然後定義手勢

private GestureDetector gestureScanner; 

手勢添加到您的onCreate gestureScanner = new GestureDetector(this); 這是手勢的方法

@Override 
public boolean onTouchEvent(final MotionEvent event) 
{ 
    return gestureScanner.onTouchEvent(event); 
} 
public boolean onDown(final MotionEvent e) 
{ 

    return true; 
} 
public boolean onFling(final MotionEvent e1, final MotionEvent e2, 
     final float velocityX, final float velocityY) 
{ 
    return true; 
} 
public void onLongPress(final MotionEvent e) { 

} 
public boolean onScroll(final MotionEvent e1, final MotionEvent e2, 
     final float distanceX, final float distanceY) 
{ 
    return true; 
} 
public void onShowPress(final MotionEvent e) 
{ 

} 
public boolean onSingleTapUp(final MotionEvent e) 
{ 
    showHide(); // show hide the statusBar On Small Phones prefer to 
    // keep it on Single Tab Cuz On Swipe Down is Kind laggy!!! 
    // WTF !! :D 
    return true; 
} 

現在,你可以在我的onSingleTapUp(final MotionEvent e)我使用的顯示隱藏狀態欄看到。

你有六種不同的觸摸事件方式「手勢事件」選擇其中一個,並把它的意圖放在它上面,當用戶「讓我們說」滾動其他活動將啓動。

​​

給它一個嘗試,我希望它對你有好處。

+0

我得到了我的答案 – 2013-02-23 11:24:45

+0

http://misha.beshkin.lv/android-swipe-gesture-implementation/ – 2013-02-23 11:25:59

+0

高興你沒有... – k0sh 2013-02-23 11:26:54