答案 0 :(得分: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;
}
}
答案 1 :(得分: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;
}
});
答案 2 :(得分: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))
{
return true;
}
return super.onTouchEvent(event);
}
private void onLeft()
{
Intent myIntent = new Intent(MainActivity.this, LeftActivity.class);
startActivity(myIntent);
}
private void onRight()
{
Intent myIntent = new Intent(MainActivity.this, RightActivity.class);
startActivity(myIntent);
}
// Private class for gestures
private class SwipeGestureDetector extends SimpleOnGestureListener
{
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)
{
MainActivity.this.onLeft();
}
// Right swipe
else if (-diff > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
{
MainActivity.this.onRight();
}
}
catch (Exception e)
{
Log.e("MainActivity", "Error on gestures");
}
return false;
}
}
}
答案 3 :(得分:0)
移动活动不是一个好方法。使用ViewPager执行滑动。 因为当您使用活动时,切换需要时间,而您可以通过视图寻呼机中的片段轻松实现切换。
请从以下链接下载代码并根据您的需要进行修改。 https://github.com/JakeWharton/ViewPagerIndicator
答案 4 :(得分:0)
看看这个ViewPage它最适合你想要做的事情