带手势的 Android 片段 onCreateView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11421368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:21:32  来源:igfitidea点击:

Android Fragment onCreateView with Gestures

android

提问by mwillbanks

I'm attempting to utilize Gestures within a fragment; I have the following inside of a FragmentActivity which handles my details fragment. What I am attempting to have happen is when a swipe is detected on the view to replace the data inside of that view with the previous or next entry.

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

If there is a better way of handling this; I am all for it. However, what is happening here is that the onFling method is never actually called.

如果有更好的处理方法;我完全同意。然而,这里发生的事情是 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;
    }
}

回答by mwillbanks

Looks like the following issue explains this: Android: GestureDetector won't catch Gestures

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

Additionally here is the result:

另外这里是结果:

The solution is actually to Override the onDown method and return true; otherwise the gesture detector will stop and not detect the up:

解决办法其实就是重写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);
            }
        });

回答by gnemnk

a couple comments

一些评论

  1. I had to tweak my code as follows:

    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. Also, if you are using an xml file to create your view

    View v = inflater.inflate(R.layout.my_view, null, false);
    
  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);
    

make sure you are actually pressing the intended view. A nice way to exaggerate the test is to make both the width and the height to "match_parent" instead of "wrap_content" in your layout xml file.

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