2015-03-25 34 views

回答

1

您可以通過使用GestureDetector下面做,這是工作的代碼替換左,右活動與自己的活動

public class MainActivity extends Activity { 
 
    private GestureDetector gesture; 
 
    
 
    @Override 
 
    public void onCreate(Bundle savedInstanceState) 
 
    { 
 
    gesture = new GestureDetector(new SwipeGestureDetector()); 
 
    } 
 

 

 
    @Override 
 
    public boolean onTouchEvent(MotionEvent event) 
 
    { 
 
    if (gesture.onTouchEvent(event)) 
 
\t { 
 
     return true; 
 
    } 
 
    return super.onTouchEvent(event); 
 
    } 
 

 
    private void onLeft() 
 
    { 
 
    Intent myIntent = new Intent(MainActivity.this, LeftActivity.class); 
 
\t startActivity(myIntent); 
 
    } 
 

 
    private void onRight() 
 
    { 
 
    Intent myIntent = new Intent(MainActivity.this, RightActivity.class); 
 
\t startActivity(myIntent); 
 
    } 
 

 
    // Private class for gestures 
 
    private class SwipeGestureDetector extends SimpleOnGestureListener 
 
\t { 
 
\t \t private static final int SWIPE_MIN_DISTANCE = 120; 
 
\t \t private static final int SWIPE_MAX_OFF_PATH = 200; 
 
\t \t private static final int SWIPE_THRESHOLD_VELOCITY = 200; 
 

 
\t \t @Override 
 
\t \t public boolean onFling(MotionEvent e1, MotionEvent e2,float velocityX, float velocityY) 
 
\t \t { 
 
\t \t \t try 
 
\t \t \t { 
 
\t \t \t \t float diffAbs = Math.abs(e1.getY() - e2.getY()); 
 
\t \t \t \t float diff = e1.getX() - e2.getX(); 
 

 
\t \t \t \t if (diffAbs > SWIPE_MAX_OFF_PATH) 
 
\t \t \t \t return false; 
 
     
 
\t \t \t \t // Left swipe 
 
\t \t \t \t if (diff > SWIPE_MIN_DISTANCE&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
 
\t \t \t \t { 
 
\t \t \t \t \t MainActivity.this.onLeft(); 
 
\t \t \t \t } 
 
\t \t \t \t // Right swipe 
 
\t \t \t \t 
 
\t \t \t \t else if (-diff > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
 
\t \t \t \t { 
 
\t \t \t \t \t MainActivity.this.onRight(); 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t \t catch (Exception e) 
 
\t \t \t { 
 
\t \t \t \t Log.e("MainActivity", "Error on gestures"); 
 
\t \t \t } 
 
\t \t \t return false; 
 
\t \t } 
 
\t } 
 
}

+0

謝謝你的科特作品:) – wdfeww 2015-03-27 10:51:45

+0

wdfeww如果代碼對你有幫助,那麼請投我的答案以及 – kashif181 2015-03-31 07:25:05

+0

我需要15個代表投票... – wdfeww 2015-03-31 10:43:24

3
public class OnSwipeTouchListener implements OnTouchListener { 

private final GestureDetector gestureDetector; 
private SwipeGestureInterface gestureInterface; 
private boolean left, right, top, bottom; 
private boolean doubleTap; 
private int x, y; 

public OnSwipeTouchListener(Context ctx, boolean left, boolean right, 
     boolean top, boolean bottom, boolean doubleTap, 
     SwipeGestureInterface gestureInterface) { 
    gestureDetector = new GestureDetector(ctx, new GestureListener()); 
    this.gestureInterface = gestureInterface; 
    this.left = left; 
    this.right = right; 
    this.top = top; 
    this.bottom = bottom; 
    this.doubleTap = doubleTap; 
} 

private final class GestureListener extends SimpleOnGestureListener { 

    private static final int SWIPE_THRESHOLD = 100; 
    private static final int SWIPE_VELOCITY_THRESHOLD = 120; 

    @Override 
    public boolean onDown(MotionEvent e) { 
     setX((int) e.getX()); 
     setY((int) e.getY()); 
     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(); 
     } 

     Utils.log("onfling", "val Y:" + velocityY); 
     return result; 
    } 

    @Override 
    public boolean onDoubleTap(MotionEvent e) { 
     // TODO Auto-generated method stub 
     if (gestureInterface != null && doubleTap) { 
      gestureInterface.onDoubleTap(); 
      return true; 
     } 
     return false; 
    } 

} 

public void onSwipeRight() { 
    if (gestureInterface != null && right) 
     gestureInterface.onSwipeRight(); 
} 

public void onSwipeLeft() { 
    if (gestureInterface != null && left) 
     gestureInterface.onSwipeLeft(); 
} 

public void onSwipeTop() { 
    if (gestureInterface != null && top) 
     gestureInterface.onSwipeTop(); 
} 

public void onSwipeBottom() { 
    if (gestureInterface != null && bottom) 
     gestureInterface.onSwipeBottom(); 
} 

@SuppressLint("ClickableViewAccessibility") 
@Override 
public boolean onTouch(View arg0, MotionEvent event) { 
    // TODO Auto-generated method stub 
    gestureDetector.onTouchEvent(event); 
    return false; 
} 

public int getX() { 
    return x; 
} 

public void setX(int x) { 
    this.x = x; 
} 

public int getY() { 
    return y; 
} 

public void setY(int y) { 
    this.y = y; 
} 

}

+0

使用滑動手勢檢測... – 2015-03-25 11:01:50

1

試試這個,

yourLayout.setOnTouchListener(new OnTouchListener() { 

    int downX, upX; 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 

     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      downX = (int) event.getX(); 
      Log.i("event.getX()", " downX " + downX); 
      return true; 
     } 

     else if (event.getAction() == MotionEvent.ACTION_UP) { 
      upX = (int) event.getX(); 
      Log.i("event.getX()", " upX " + downX); 
      if (upX - downX > 100) { 

       // swipe right 
        //Call activity 


      } 

      else if (downX - upX > -100) { 

       // swipe left 
       call activity 
      } 
      return true; 

      } 
      return false; 
     } 
    }); 
+0

或者你也使用viewpager爲簡單的方法... .... – 2015-03-25 11:06:49

0

搬家活動不是一個好方法。使用ViewPager執行刷卡。 因爲當你使用活動時,切換需要時間,而你可以通過分段查看尋呼機輕鬆實現。

請從下面的鏈接下載代碼並根據您的需要進行修改。 https://github.com/JakeWharton/ViewPagerIndicator

0

看一看這個ViewPage它最適合與你正在嘗試做的