我有 4 个活动,有 4 个类和 xml 布局;我想在 4 个活动之间滑动!!我使用另一篇文章中的这段代码(这篇文章:网格布局上的 Fling 手势检测)
我想在打开新活动时触摸 leftToright 或 rightToleft 滑动。
我的代码哪里错了:
我的代码:
package com.package110.Y;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class ActivitySwipeDetectorActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// how to set this 3 line to in my code Or change or update it
//ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this);
//lowestLayout = (RelativeLayout)this.findViewById(R.id.lowestLayout);
//lowestLayout.setOnTouchListener(activitySwipeDetector);
}
public class ActivitySwipeDetector implements View.OnTouchListener {
Intent left = new Intent(getApplicationContext(), left.class);
Intent right = new Intent(getApplicationContext(), right.class);
Intent top = new Intent(getApplicationContext(), top.class);
Intent bottom = new Intent(getApplicationContext(), bottom.class);
static final String logTag = "ActivitySwipeDetector";
private Activity activity;
static final int MIN_DISTANCE = 100;
private float downX, downY, upX, upY;
public ActivitySwipeDetector(Activity activity){
this.activity = activity;
}
public void onRightToLeftSwipe(){
Log.i(logTag, "RightToLeftSwipe!");
activity.startActivity(right);
}
public void onLeftToRightSwipe(){
Log.i(logTag, "LeftToRightSwipe!");
activity.startActivity(left);
}
public void onTopToBottomSwipe(){
Log.i(logTag, "onTopToBottomSwipe!");
activity.startActivity(top);
}
public void onBottomToTopSwipe(){
Log.i(logTag, "onBottomToTopSwipe!");
activity.startActivity(bottom);
}
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
downY = event.getY();
return true;
}
case MotionEvent.ACTION_UP: {
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
// 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
}
// swipe vertical?
if(Math.abs(deltaY) > MIN_DISTANCE){
// top or down
if(deltaY < 0) { this.onTopToBottomSwipe(); return true; }
if(deltaY > 0) { this.onBottomToTopSwipe(); 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;
}
}
}
解释:我的应用程序被强制关闭!我的谷歌 API 适用于 android 3.0;我的应用程序有问题吗?我也尝试了这段代码,它强制关闭!!!!
另一个代码:
package ir.package110.Y;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
public class ActivitySwipeDetectorActivity extends Activity {
private GestureDetector gestureDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ...
gestureDetector = new GestureDetector(new SwipeGestureDetector());
}
/* ... */
Intent right = new Intent(ActivitySwipeDetectorActivity.this.getBaseContext(), right.class);
Intent left = new Intent(ActivitySwipeDetectorActivity.this.getBaseContext(), left.class);
private void onLeftSwipe() {
// Do something
startActivity(left);
}
private void onRightSwipe() {
startActivity(right);
// Do something
}
// Private class for gestures
private class SwipeGestureDetector extends SimpleOnGestureListener {
// Swipe properties, you can change it to make the swipe
// longer or shorter and speed
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 200;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
try {
float diffAbs = Math.abs(e1.getY() - e2.getY());
float diff = e1.getX() - e2.getX();
if (diffAbs > SWIPE_MAX_OFF_PATH)
return false;
// Left swipe
if (diff > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
ActivitySwipeDetectorActivity.this.onLeftSwipe();
// Right swipe
} else if (-diff > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
ActivitySwipeDetectorActivity.this.onRightSwipe();
}
} catch (Exception e) {
Log.e("YourActivity", "Error on gestures");
}
return false;
}
}
}