2012-07-10 116 views
17

我試圖利用片段內的手勢;我在處理我的細節片段的FragmentActivity中有以下內容。我試圖發生的事情是在視圖上檢測到滑動以用上一個或下一個條目替換該視圖內部的數據。Android的片段onCreateView與手勢

如果有更好的處理方法,我完全同意。然而,這裏發生的是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; 
    } 
} 

回答

43

貌似以下問題解釋如下: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); 
      } 
     }); 
+3

只是好奇:爲什麼不參考手勢丟失? – 2012-09-27 07:18:01

+1

@LemLordjeKo爲什麼對'gesture'的引用會丟失? 'gesture'作爲我們創建的'View.OnTouchListener'的參考,並且通過視圖本身'v'來保存。 Java已經足夠成熟,如果有對象的路徑,它幾乎肯定不會得到GC'd。 – bclymer 2013-12-17 16:02:50

+0

上述代碼段需要以下導入才能工作: import android.view.View.OnTouchListener; import android.view.MotionEvent; import android.view.GestureDetector; import android.util.Log; class常量public static final String APP_TAG =「mytag」; } – FuzzyAmi 2016-05-01 14:03:53

2

一對夫婦的評論

  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」。

+0

我錯過了@mwillbanks重寫onDown以返回SimpleOnGestureListener的true。我只在onSingleTapConfirmed和onDoubleTap上實現。 – gnemnk 2016-08-12 04:16:49