2012-10-15 51 views
3

我的布局XML文件中像这样安卓SimpleOnGestureListener只触发onscroll事件

.... 
gDetector = new GestureDetector(new Gestures(this)); 
    View.OnTouchListener gestureListener = new View.OnTouchListener() { 
     public boolean onTouch(View v, MotionEvent event) { 
     return gDetector.onTouchEvent(event); 
     } 
    }; 

@Override 
    public boolean onTouchEvent(MotionEvent me) { 
    return gDetector.onTouchEvent(me); 
    } 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentTop="true" 
android:layout_centerHorizontal="true" 
android:orientation="vertical"> 

<LinearLayout 
    android:id="@+id/WebviewContainer" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" > 

    <WebView 
     android:id="@+id/Webview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

</LinearLayout> 

<LinearLayout 
    android:id="@+id/BottomBtns" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <Button 
     android:id="@+id/ImageButton01" 
     android:layout_width="36px" 
     android:layout_height="36px" 
     android:layout_gravity="center_vertical" 
     android:adjustViewBounds="true" 
     android:background="@drawable/btn_left" 
     android:onClick="ClickBtn1" /> 

    <Button 
     android:id="@+id/ImageButton02" 
     android:layout_width="36px" 
     android:layout_height="36px" 
     android:layout_gravity="center_vertical" 
     android:adjustViewBounds="true" 
     android:background="@drawable/btn_home" 
     android:onClick="ClickBtn2" /> 

    <Button 
     android:id="@+id/ImageButton03" 
     android:layout_width="36px" 
     android:layout_height="36px" 
     android:layout_gravity="center_vertical|center_horizontal" 
     android:adjustViewBounds="true" 
     android:background="@drawable/btn_refresh" 
     android:onClick="ClickBtn3" /> 

    <Button 
     android:id="@+id/ImageButton04" 
     android:layout_width="36px" 
     android:layout_height="36px" 
     android:layout_gravity="center_vertical|center_horizontal" 
     android:adjustViewBounds="true" 
     android:background="@drawable/btn_right" 
     android:onClick="ClickBtn4" /> 

</LinearLayout> 

,我的主要活动.....

我的手势像这样:

package com.news; 

import android.app.Activity; 
import android.view.GestureDetector.SimpleOnGestureListener; 
import android.view.MotionEvent; 
import android.widget.Toast; 

class Gestures extends SimpleOnGestureListener { 

    private Activity a = null; 

    final int horizontalRange = 10; 

    final int verticalRange = 50; 

    final int GESTURE_LEFT = 1; 

    final int GESTURE_RIGHT = 2; 

    final int GESTURE_INVALID = 0; 

    public Gestures(Activity activity) { 
    a = activity; 
    } 

    public Gestures() { 

    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
     float velocityY) { 
    Toast.makeText(a.getApplicationContext(), "FLING", Toast.LENGTH_SHORT).show(); 
    return false; 
    } 

    @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
     float distanceY) { 
    if (distanceY<horizontalRange && distanceX>verticalRange) { 
     android.webkit.WebView webView = (android.webkit.WebView)a.getWindow().getDecorView().findViewById(R.id.Webview); 
     webView.goBack(); 
     //Toast.makeText(a.getApplicationContext(), "Left" + distanceX, Toast.LENGTH_SHORT).show(); 
    } 
    else if(distanceY<horizontalRange && distanceX < -verticalRange) { 
     android.webkit.WebView webView = (android.webkit.WebView)a.getWindow().getDecorView().findViewById(R.id.Webview); 
     webView.goForward(); 
    } 
    return false; 
    } 

    @Override 
    public boolean onDown(MotionEvent me) { 
    return true; 
    } 
} 

有一个很奇怪的情况。只有在滚动事件被触发~~ onFling事件从未被触发~~我尝试了很多方法。但它没有工作〜 任何专家知道为什么?

非常感谢~~~

回答

0

以防万一你没有找到答案。我面临同样的问题,我的解决方案是here

更改此方法并返回true

@Override 
    public boolean onTouchEvent(MotionEvent me) { 
    gDetector.onTouchEvent(me); 
    return true; 
    } 

希望这有助于!