2016-02-12 47 views
1

我重視我的onCreate()方法touchlistener和gesturelistener後工作。安卓:DoubleTap不改變方向

當我槍王我的標題欄,在縱向模式下,一切工作正常。但是當我將屏幕旋轉到橫向模式時,雙擊不再被檢測到。然而,聽衆仍然被呼叫。當我將屏幕旋轉回肖像模式時,雙擊再次工作。

我的聽衆:

//Add double click gesture listener to Title Bar. 
     final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() { 
      public boolean onDoubleTap(MotionEvent e) { 
       myMethod(); 
       return true; 
      } 
     }); 
     TextView tv = (TextView) findViewById(R.id.tv_title); 
     tv.setOnTouchListener(new OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 
       return gestureDetector.onTouchEvent(event); 
      } 
     }); 
+0

添加'公共無效onLongPress(MotionEvent五)'從兩個方向 – Mcloving

+0

作品是您使用方法:'公共無效onConfigurationChanged(配置NEWCONFIG){'?如果是的話......你是否設置了一個新的contentView?原因改變方向重新開始活動,讓您的內容標題欄是最有可能找不到了 – Strider

+0

請給我們一些更多的代碼 – Strider

回答

2

已經意識到,長按總是調用,它給了我更大的空間尋找一個現有的問題/答案。

我發現這個question.但是,答案與我的情況無關,它確實存在問題,如果我已正確實施OnTouchListener

要去android docs我注意到,我應該可能返回true,而不是姿態的結果。 (OnLongClick返回void!),它可能會一直強迫它消耗該結果。

的Android文檔指定的OnTouchListener實現如下:

View myView = findViewById(R.id.my_view); 
myView.setOnTouchListener(new OnTouchListener() { 
    public boolean onTouch(View v, MotionEvent event) { 
     // ... Respond to touch events  
     return true; 
    } 
}); 

我的代碼更改爲以下解決了該問題

//Add double click gesture listener to Title Bar. 
    final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() { 
     public boolean onDoubleTap(MotionEvent e) { 
      myMethod(); 
      return true; 
     } 
    }); 
    TextView tv = (TextView) findViewById(R.id.tv_title); 
    tv.setOnTouchListener(new OnTouchListener() { 
     public boolean onTouch(View v, MotionEvent event) { 
      gestureDetector.onTouchEvent(event); 
      return true; // ADDED THIS 
     } 
    }); 

但是爲什麼它在肖像,而不是景觀, 我不知道。

+1

好,ü找到一個解決辦法 – Strider

+0

。@Strider感謝試圖幫助:)不過百思不得其解它是如何工作的一個方向。但是,這種修復方法在兩者中均可行 – Mcloving