2014-01-14 61 views
1

在我的代碼中,從右到左刷卡正常工作,但從左到右刷卡不起作用。我不知道原因是什麼。刷卡無法正常工作

代碼 -

public class Types extends Activity{ 

    RelativeLayout layout1; 
    int[] backgroundResId; 
    int currentIndex=0; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_type); 
     backgroundResId=new int[]{R.drawable.back,R.drawable.bg,R.drawable.frame1}; 
     layout1 = (RelativeLayout) findViewById(R.id.layout1); 
     changeBackground(); 
     ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this); 
     layout1.setOnTouchListener(activitySwipeDetector); 

    } 


    private void changeBackground(){ 
     layout1.setBackgroundResource(backgroundResId[currentIndex]); 
    } 

    public class ActivitySwipeDetector implements View.OnTouchListener { 

     static final String logTag = "ActivitySwipeDetector"; 
     static final int MIN_DISTANCE = 100; 
     private float downX, upX; 
     private Activity activity; 

     public ActivitySwipeDetector(Activity activity){ 
      this.activity = activity; 
     } 

     public void onRightToLeftSwipe(){ 
      Log.i(logTag, "RightToLeftSwipe!"); 
      currentIndex++; 
      if(currentIndex<backgroundResId.length){ 
       changeBackground(); 
      } 
     } 

     public void onLeftToRightSwipe(){ 
      Log.i(logTag, "LeftToRightSwipe!"); 
      if(currentIndex>0){ 
       changeBackground(); 
      } 
     } 

     public boolean onTouch(View v, MotionEvent event) { 
      switch(event.getAction()){ 
       case MotionEvent.ACTION_DOWN: { 
        downX = event.getX(); 
        return true; 
       } 
       case MotionEvent.ACTION_UP: { 
        upX = event.getX(); 

        float deltaX = downX - upX; 

        // swipe horizontal? 
        if(Math.abs(deltaX) > MIN_DISTANCE){ 
         // left or right 
         if(deltaX < 0) { this.onLeftToRightSwipe(); return true; } 
         if(deltaX > 0) { this.onRightToLeftSwipe(); return true; } 
        } 
        else { 
          Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE); 
          return false; // We don't consume the event 
        } 


        return true; 
       } 
      } 
      return false; 
     } 

     } 

在此行中private Activity activity;我得到警告。爲什麼?

+0

爲什麼你不使用'SimpleOnGestureListener'? –

+0

我已經嘗試過,但沒有工作,每個人只是給我參考其他鏈接。 –

+0

http://stackoverflow.com/questions/4139288/android-how-to-handle-right-to-left-swipe-gestures/12938787#12938787 –

回答

1

你忘了零下CURRENTINDEX

public void onLeftToRightSwipe(){ 
      Log.i(logTag, "LeftToRightSwipe!"); 
      if(currentIndex>0){ 
       currentIndex--; 
       changeBackground(); 
      } 
     } 
+0

不錯的一個。加工。 –

1

這是一個樣本與SimpleOnGestureListener

GestureDetector gestureDetector = new GestureDetector(this, new MyGestureDetector()); 
layout1 = (RelativeLayout) findViewById(R.id.layout1); // main Layout 

然後

layout1.setOnTouchListener(new View.OnTouchListener() { 
     public boolean onTouch(View v, MotionEvent event) { 
       if (gestureDetector.onTouchEvent(event)) { 
        return true; 
       } 
       return false; 
      } 
     }); 

和MyGestureDetector工作:

class MyGestureDetector extends SimpleOnGestureListener { 
    SWIPE_THRESHOLD_VELOCITY = 200; 
    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
      float velocityY) { 


     if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE 
       && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
      // i think this is swipe to right 

     } 

     else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE 
       && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       //and i think this is swipe to left 

     } 

     return false; 
    } 

    // It is necessary to return true from onDown for the onFling event to 
    // register 
    @Override 
    public boolean onDown(MotionEvent e) { 
     return true; 
    } 

} 

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    // TODO Auto-generated method stub 
    return false; 
} 
+0

但在我的代碼什麼是錯的? –

+0

我不認爲你的方式是正確的,因爲當你把你的手指放在屏幕上的onDown調用,而當你的手指在屏幕上你的'downX'已經更新,如果你想用你的方式,我認爲你可以用一個布爾值只捕捉一次'downX',但真的不知道是什麼問題 –

+0

你測試'SimpleOnGestureListener'的id?這有效嗎? –