使用平滑动画滑动活动

时间:2014-02-03 12:51:58

标签: android swipe

我正在努力解决这个问题...

结构是 -

[1]<---->[2]<-------->[3]<-------->[4]

其中[] =活动...(需要滑动视图的平滑动画)
我不需要的东西-----&gt;按下Android设备时闪烁和后退堆栈......

活动1滑动为2,进一步滑动为3,但它会闪烁而不是滑动...

这是代码......

  public ActivitySwipeDetector(SwipeInterface activity){
    this.activity = activity;
}

public void onRightToLeftSwipe(View v){
    Log.i(logTag, "RightToLeftSwipe!");
    activity.right2left(v);
}

public void onLeftToRightSwipe(View v){
    Log.i(logTag, "LeftToRightSwipe!");
    activity.left2right(v);
}

public void onTopToBottomSwipe(View v){
    Log.i(logTag, "onTopToBottomSwipe!");
    activity.top2bottom(v);
}

public void onBottomToTopSwipe(View v){
    Log.i(logTag, "onBottomToTopSwipe!");
    activity.bottom2top(v);
}

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(v); return true; }
            if(deltaX > 0) { this.onRightToLeftSwipe(v); return true; }
        }
        else {
            Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
        }

        // swipe vertical?
        if(Math.abs(deltaY) > MIN_DISTANCE){
            // top or down
            if(deltaY < 0) { this.onTopToBottomSwipe(v); return true; }
            if(deltaY > 0) { this.onBottomToTopSwipe(v); return true; }
        }
        else {
            Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least       " + MIN_DISTANCE);
            v.performClick();
        }
    }
    }
      return false;
  }
}

1 个答案:

答案 0 :(得分:0)

我建议您使用片段而不是活动。使用viewpager可以轻松完成您想要的任务。我第一次使用viewpager时使用了本教程:

http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

以下是官方链接:

http://developer.android.com/training/implementing-navigation/lateral.html

希望这有帮助!