0

我正在尝试进行滑动活动,但没有这样做。下面将是我的代码,但它什么也不做,甚至没有给出任何错误。我不知道该怎么办。

    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 类被贬低,这是这个程序没有运行的原因。

4

1 回答 1

0

看看这个,官方教程 http://android-developers.blogspot.com/2011/08/horizo​​ntal-view-swiping-with-viewpager.html

于 2013-08-16T07:06:00.197 回答