在一个片段内未检测到android swipe

时间:2012-06-04 01:43:59

标签: android swipe gesture-recognition

我有一个基于标签主机并使用片段的项目

在一个标签上它有一个listFragment,用户点击一个单元格并转到另一个片段,

从那里用户可以在该标签的片段之间滑动,

我让项目工作,直到我进入listFragment的单元格,

我需要检测向左或向右滑动以相应地更改碎片,

这里是我的代码:

public class PrepareSkin extends Fragment implements OnGestureListener{

      private static final int SWIPE_MIN_DISTANCE = 120;
       private static final int SWIPE_MAX_OFF_PATH = 250;
       private static final int SWIPE_THRESHOLD_VELOCITY = 200;

       private GestureDetector gestureScanner;


    @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
       Bundle savedInstanceState) {


        gestureScanner = new GestureDetector(this);


      // TODO Auto-generated method stub
      View myFragmentView = inflater.inflate(R.layout.prepare_skin, container, false);

       Log.i("Mirko", "CREATO <<");


      return myFragmentView;
     }

       public boolean onTouchEvent(MotionEvent me)
       {
        return gestureScanner.onTouchEvent(me);
       }

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        try {
               if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                   return false;
               // right to left swipe
               if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                   Log.i("Mirko", "LEFTERS <<");


               }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                   Log.i("Mirko", "rIGTHERS >>");

               }
               else if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
               }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
               }
           } catch (Exception e) {
               // nothing
           }

                   return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

}

那为什么我的手势识别器无法正常工作?

感谢,

1 个答案:

答案 0 :(得分:1)

此链接似乎在您的小巷中:Android Fragment onCreateView with Gestures

虽然他们没有在他们的类中实现OnGestureListener,而是将clickListener附加到他们的整个View中,然后以这种方式触发Gesture Listener,它仍然应用于你的代码片段。

另一个快速修复可能是您将onDown()返回为false - 也许您应该将其返回为true。我知道,如果onUp()之前未返回onDown(),则Android无法识别true