2013-08-16 162 views
0

我正在嘗試執行滑動活動,但未能如此操作。下面是我的代碼,但它什麼都不做,甚至沒有給出任何錯誤。我不知道該怎麼辦。在活動之間滑動,從一個活動滑到另一個活動

final View lay_first=(View) findViewById(R.id.lay_first); 

    lay_first.setOnTouchListener(new OnSwipeTouchListener(){ 
     @Override 
     public boolean onSwipeLeft() { 
      Intent intent=new Intent(SlideWindow.this,MainActivity.class); 
      startActivity(intent); 
      overridePendingTransition(R.anim.right_to_left, R.anim.rl2); 
       return true; 
     } 
     @Override 
     public boolean onSwipeRight() { 
     Intent intent=new Intent(SlideWindow.this,MainActivity.class); 
     startActivity(intent); 
     overridePendingTransition(R.anim.right_to_left, R.anim.rl2); 
      return true; 
     } 
     @Override 
     public boolean onSwipeBottom() { 
      super.onSwipeBottom(); 
      return true; 
     } 
     @Override 
     public boolean onSwipeTop() { 
      super.onSwipeTop(); 
      return true; 
     } 
}); 
} 
public class OnSwipeTouchListener implements OnTouchListener{  
    @SuppressWarnings("deprecation") 
    GestureDetector gestureDetector=new GestureDetector(new GestureListener()); 
    @Override 
    public boolean onTouch(View v, MotionEvent event) {   
     if(gestureDetector.onTouchEvent(event)) 
     { 
      return true; 
     } 
     return false; 
    } 
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 super.onDown(e); 
     } 

     @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) 
        { 
         result=onSwipeRight(); 
        } 
        else 
        { 
         result=onSwipeLeft(); 
        } 
       } 
      } 
      else 
      { 
      if(Math.abs(diffY)>SWIPE_THRESHOLD && Math.abs(velocityY)>SWIPE_VELOCITY_THRESHOLD) 
       { 
        if(diffY>0) 
        { 
         result=onSwipeBottom(); 
        } 
        else 
        { 
         result=onSwipeTop(); 
        }  } 
      } 
     }catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
      return result; 
     } } 
     public boolean onSwipeRight() 
     { 
    return false; 
      } 
     public boolean onSwipeLeft() 
     { 
    return false;     
     } 
     public boolean onSwipeBottom() { 
      return false; 
    } 
     public boolean onSwipeTop() { 
      return false; 
    } 
}} 

但是當我上的TouchEvent簡單地做幻燈片是running.But當我加入了手勢不是這樣,因爲GestureDetector類depriciated這是該程序不運行的原因。

+0

刷卡動作不執行? – Piyush

+0

爲什麼你不使用viewpager與片段? –

+0

要實現這個刷卡來改變活動有什麼意義?橫向滑動瀏覽器不能很好地工作? –

回答