2012-10-26 107 views
26

我有一個問題,我的方法方法的onTouchEvent不會被調用

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    return gestureDetector.onTouchEvent(event); 
} 

是較新的調用。任何想法爲什麼是這樣?我正在構建一個谷歌的api 4.0.3應用程序,並且我很喜歡爲我的viewFliper啓用swipe。但是,如果接觸更新,它不能工作。

BTW:

public class MainActivity extends SherlockMapActivity implements ActionBar.TabListener { 

這就是我的活動的聲明。並檢測刷我已經實現:

SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener(){ 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) { 

     float sensitvity = 50; 
     if((e1.getX() - e2.getX()) > sensitvity){ 
      SwipeLeft(); 
     }else if((e2.getX() - e1.getX()) > sensitvity){ 
      SwipeRight(); 
     } 

     return true; 
    } 

}; 
GestureDetector gestureDetector= new GestureDetector(simpleOnGestureListener); 

謝謝你!

編輯:

的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/main_view" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" > 

<ViewFlipper 
    android:id="@+id/ViewFlipper" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#ffffff" > 

    <include 
     android:layout_height="match_parent" 
     layout="@layout/mymain" /> 

    <include layout="@layout/secondmain" /> 
    <include layout="@layout/thirdmain" /> 
    <include layout="@layout/fourthmain" /> 
</ViewFlipper> 
</LinearLayout> 

EDIT2:我所有的包含的佈局都實行滾動型是有可能,滾動採取的事件和處理它們。如何檢測手勢?

+0

好吧,現在我確定問題是scrollview句柄觸及,所以無論如何忽略這一點,但是滾動可用? – gabrjan

回答

40

我找到了一個完美的父視圖的requestDisallowInterceptTouchEvent解。我實施了新方法:

@Override 
public boolean dispatchTouchEvent(MotionEvent event) { 

    View v = getCurrentFocus(); 
    boolean ret = super.dispatchTouchEvent(event); 

現在它一切正常!

編輯:

我的最終代碼:

@Override 
public boolean dispatchTouchEvent(MotionEvent event) { 
    View v = getCurrentFocus(); 
    if (v instanceof EditText) { 
     View w = getCurrentFocus(); 
     int scrcoords[] = new int[2]; 
     w.getLocationOnScreen(scrcoords); 
     float x = event.getRawX() + w.getLeft() - scrcoords[0]; 
     float y = event.getRawY() + w.getTop() - scrcoords[1]; 
     if (event.getAction() == MotionEvent.ACTION_UP 
       && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w 
         .getBottom())) { 

      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(getWindow().getCurrentFocus() 
        .getWindowToken(), 0); 
     } 
    } 
    boolean ret = super.dispatchTouchEvent(event); 
    return ret; 
} 
+0

謝謝,這是一個比@Chaosit更容易的解決方案。 –

+0

嗨 - 你在哪裏實施這種新方法?它會返回什麼? – Jona

+0

檢查編輯答案。我在我的MainActivity.java文件中實現了該功能。或者更好地說在主要的應用程序類。 – gabrjan

1

所有手勢和觸摸事件都轉到視圖層次結構中處理它的最低元素。 因此,如果您在包含佈局中有任何聽衆,則可以致電((TypeOfParrent)yourView.getParent()).onTouchEvent(event)將事件委派給您想要的處理程序。

ADD:我會重新邀請您使用ViewPager來翻閱視圖。 在ViewPager中你不需要實現你自己的onGestureListener。

http://www.edumobile.org/android/android-beginner-tutorials/view-pager-example-in-android-development/

http://developer.android.com/reference/android/support/v4/view/ViewPager.html

5

好了,現在我敢肯定,問題是,滾動型手柄接觸,所以無論如何忽略,但可以滾動繳費?

是的,這是問題,當android處理觸摸事件時,每個事件從孩子到父母,所以首先它由ViewFlipper處理,但它然後到ScrollView。因此,您必須實現getParent()。requestDisallowInterceptTouchEvent(true)(請參閱ViewParent class)以使所有觸摸事件由ViewFlipper處理,然後簡單地檢測手勢的方向(如果不是水平的話則翻轉視圖),然後將觸摸事件傳遞給ScrollView或只是滾動滾動型編程

編輯:你也可以在你的ViewFlipper在這個監聽器觸發GestureDetector.onTouchEvent(事件)實施OnTouchListener,但是這也需要設置爲true

5

正如我在Gabrjan的這篇文章的評論中寫道,這一火災不斷,而感人的畫面,實際上是一個簡單的方法來只觸摸下壓的事件:

@Override 
public boolean dispatchTouchEvent(MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
     System.out.println("TOUCH DOWN!"); 
     //set immersive mode here, or whatever... 
    } 
    return super.dispatchTouchEvent(event); 
} 

這是對我非常有用把Android系統成爲沉浸式模式每當屏幕的任何部分,無論是哪個元素感動。但我不想重複設置沉浸式模式!

相關問題