2012-10-26 84 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系统成为沉浸式模式每当屏幕的任何部分,无论是哪个元素感动。但我不想重复设置沉浸式模式!

相关问题