Android-Detect滑动手势在Fragment中不起作用

时间:2015-12-31 07:57:00

标签: java android android-fragments touch gesture

我试图像这样实施SwipeGesture

public class Fragmentforalbumartplayeractivity extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.album_artplayeractivity,container, false);


    final GestureDetector gesture = new GestureDetector(getActivity(),
        new GestureDetector.SimpleOnGestureListener() {

            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
                Log.i(Constants.APP_TAG, "onFling has been called!");
                final int SWIPE_MIN_DISTANCE = 120;
                final int SWIPE_MAX_OFF_PATH = 250;
                final int SWIPE_THRESHOLD_VELOCITY = 200;
                try {
                    if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                        return false;
                    if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Log.i(Constants.APP_TAG, "Right to Left");
                    } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                      Log.i(Constants.APP_TAG, "Left to Right");
                    }
                } catch (Exception e) {
                    // nothing
                }
                return super.onFling(e1, e2, velocityX, velocityY);
            }
        });

        root.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gesture.onTouchEvent(event);
            }
        });

    return root;
 }

但是,当我从左向右或从右向左滑动时,没有检测到滑动...我还想检测向上向下滑动向下向上扫描。

知道为什么没有检测到滑动?

提前谢谢你:)

0 个答案:

没有答案