我想在两个活动之间创建简单的滑动手势,我已经搜索了很多类似下面的内容。但我怀疑的是我如何使用滑动手势滑动活动或视图!!
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(SelectFilterActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(SelectFilterActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
}
希望有人能帮助我@Thanks!
答案 0 :(得分:6)
立即试用此代码..
page = (LinearLayout) findViewById(R.id.flipper);
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
super.dispatchTouchEvent(event);
return gestureDetector.onTouchEvent(event);
}
SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
float sensitvity = 50;
if ((e1.getX() - e2.getX()) > sensitvity) {
SwipeLeft();
} else if ((e2.getX() - e1.getX()) > sensitvity) {
SwipeRight();
}
return true;
}
};
GestureDetector gestureDetector = new GestureDetector(
simpleOnGestureListener);
private void SwipeLeft() {
page.setInAnimation(animFlipInForeward);
page.setOutAnimation(animFlipOutForeward);
page.showNext();
}
private void SwipeRight() {
page.setInAnimation(animFlipInBackward);
page.setOutAnimation(animFlipOutBackward);
page.showPrevious();
}
答案 1 :(得分:1)
implement your activity with OnGestureListener
yourview.setOnTouchListener(touchListener);
实现touchListener,如下所示
OnTouchListener touchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureScanner.onTouchEvent(event);
return false;
}
};
用于滑动的onFling方法。
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
// if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
// return false;
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
viewFlipper.setInAnimation(animFlipInPrevious);
viewFlipper.setOutAnimation(animFlipOutPrevious);
Globle.falg =false;
if (count != 2) {
if (count == 2) {
count = 0;
} else {
count = count + 1;
}
if (currentView == 0) {
viewFlipper.showNext();
currentView = 1;
} else if (currentView == 1) {
viewFlipper.showNext();
currentView = 2;
} else {
currentView = 0;
viewFlipper.showNext();
}
}
count = viewFlipper.getDisplayedChild();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
viewFlipper.setInAnimation(animFlipInNext);
viewFlipper.setOutAnimation(animFlipOutNext);
Globle.falg =false;
if (count != 0) {
if (count == 0) {
count = 2;
} else {
count = count - 1;
}
if (currentView == 2) {
currentView = 1;
viewFlipper.showPrevious();
} else if (currentView == 1) {
currentView = 0;
viewFlipper.showPrevious();
} else {
currentView = 2;
viewFlipper.showPrevious();
}
}
count = viewFlipper.getDisplayedChild();
}
} catch (Exception e) {
// nothing
}
return false;
}
}
如上面的代码,您可以更改您的视图,在我的情况下有三个视图,但您只需要两个以上更改计数值。 这对你有帮助。
答案 2 :(得分:1)
public class Main extends Activity implements OnGestureListener {
private GestureDetector gDetector;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gDetector = new GestureDetector(this);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
return false;
}
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
((ImageView) findViewById(R.id.image_place_holder))
.setImageResource(R.drawable.down);
}
// left to right swipe
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
((ImageView) findViewById(R.id.image_place_holder))
.setImageResource(R.drawable.up);
}
} catch (Exception e) {
}
return false;
}
}
答案 3 :(得分:0)