Android Fragment onCreateView with Gestures

前端 未结 2 1009
轮回少年
轮回少年 2020-12-02 20:57

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 hap

相关标签:
2条回答
  • 2020-12-02 21:11

    Looks like the following issue explains this: 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:

            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);
                }
            });
    
    0 讨论(0)
  • 2020-12-02 21:11

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

    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.

    0 讨论(0)
提交回复
热议问题