25

我正在尝试在片段中使用手势;我在处理我的详细信息片段的 FragmentActivity 中有以下内容。我试图发生的是当在视图上检测到滑动以用上一个或下一个条目替换该视图内的数据时。

如果有更好的处理方法;我完全同意。然而,这里发生的是 onFling 方法从未真正被调用过。

public static class DetailsFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }
        View v = inflater.inflate(R.layout.my_view, null, false);
        final GestureDetector gesture = new GestureDetector(getActivity(),
            new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                    float velocityY) {

                    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");
                            titles.showDetails(getPosition() - 1);
                        }
                    } catch (Exception e) {
                        // nothing
                    }
                    return super.onFling(e1, e2, velocityX, velocityY);
                }
            });

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

        return v;
    }
}
4

2 回答 2

46

看起来以下问题解释了这一点:Android:GestureDetector won't catch Gestures

另外这里是结果:

解决办法其实就是重写onDown方法,返回true;否则手势检测器将停止并且不会检测到向上:

        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);
                }
            });

        v.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gesture.onTouchEvent(event);
            }
        });
于 2012-07-10T20:39:07.853 回答
8

一些评论

  1. 我不得不调整我的代码如下:

    v.setOnTouchListener(new View.OnTouchListener() {
        @Override        
        public boolean onTouch(View v, MotionEvent event) {
    
            // return gesture.onTouchEvent(event);
    
            gesture.onTouchEvent(event);
            return true; // <-- this line made the difference
        }
    });
    
  2. 此外,如果您使用 xml 文件来创建视图

    View v = inflater.inflate(R.layout.my_view, null, false);
    

确保您实际上正在按下预期的视图。夸大测试的一个好方法是在布局 xml 文件中将宽度和高度设置为“match_parent”而不是“wrap_content”。

于 2016-08-12T04:08:43.700 回答