在活动之间滑动,从一个活动滑动到另一个活动

时间:2013-08-16 06:24:30

标签: android android-animation swipe swipe-gesture

我正在尝试刷卡活动但未能这样做。下面将是我的代码,但它什么都不做,甚至没有给出任何错误。我不知道该怎么做。

    final View lay_first=(View) findViewById(R.id.lay_first);

    lay_first.setOnTouchListener(new OnSwipeTouchListener(){
        @Override
        public boolean onSwipeLeft() {
            Intent intent=new Intent(SlideWindow.this,MainActivity.class);
            startActivity(intent);
            overridePendingTransition(R.anim.right_to_left, R.anim.rl2);
                return true;
        }
        @Override
        public boolean onSwipeRight() {
        Intent intent=new Intent(SlideWindow.this,MainActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.right_to_left, R.anim.rl2);
            return true;
        }
        @Override
        public boolean onSwipeBottom() {
            super.onSwipeBottom();
            return true;
        }
        @Override
        public boolean onSwipeTop() {
            super.onSwipeTop();
            return true;
        }
});
}
public class OnSwipeTouchListener implements OnTouchListener{       
     @SuppressWarnings("deprecation")
    GestureDetector gestureDetector=new GestureDetector(new GestureListener());
    @Override
    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 super.onDown(e);
        }

         @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)
                    {
                        result=onSwipeRight();
                    }
                    else
                    {
                        result=onSwipeLeft();
                    }
                }
            }
            else
            {
            if(Math.abs(diffY)>SWIPE_THRESHOLD && Math.abs(velocityY)>SWIPE_VELOCITY_THRESHOLD)
                {
                    if(diffY>0)
                    {
                        result=onSwipeBottom();
                    }
                    else
                    {
                        result=onSwipeTop();
                    }       }
            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }
            return result;
        }   }
       public boolean onSwipeRight()
         {
    return false;
          }
         public boolean onSwipeLeft()
         {
    return false;                
         }
         public boolean onSwipeBottom() {
             return false;
     }
         public boolean onSwipeTop() {
             return false;
     }
}}

但是当我在touchevent上做幻灯片时它正在运行。但是当我添加手势时它不是,因为GestureDetector类被解除了这就是这个程序没有运行的原因。

1 个答案:

答案 0 :(得分:0)