gpt4 book ai didi

android - 在android中从左向右和从右向左滑动

转载 作者:搜寻专家 更新时间:2023-11-01 09:03:49 26 4
gpt4 key购买 nike

我有 SwipeActivity。当我从右向左滑动时,我想进入 Activity B,当我从左向右滑动时,我想进入 Activity C。但是我的代码不起作用。请帮助我。

我的 SwipeActivity 是:

公共(public)类 SwipeActivity 扩展 Activity {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
View mainview = (View) findViewById(R.id.mainView);

// Set the touch listener for the main view to be our custom gesture listener
mainview.setOnTouchListener(new View.OnTouchListener() {
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 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();
}
}
}

} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}

public void onSwipeRight() {
Intent in = new Intent(SwipeActivity.this,page1Activity.class);
startActivity(in);
}

public void onSwipeLeft() {
Intent in = new Intent(SwipeActivity.this,page2Activity.class);
startActivity(in);
}

最佳答案

我认为您可以使用 fragment ,以及在 Activity 中包含 fragment 的 ViewPager。

检查这是示例:http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/

希望对你有用。

关于android - 在android中从左向右和从右向左滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13399927/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com