android SimpleOnGestureListener只触发了onscroll事件

时间:2012-10-15 14:11:13

标签: android uigesturerecognizer onfling android-gesture

我的布局xml文件,如下

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

和我这样的主要活动

....
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);
  }

.....

我的手势是这样的:

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;
  }
}

有一种非常奇怪的情况。只有onScroll事件被触发了~~ onFling事件从未被触发~~我尝试了很多方法。但它没有用〜 任何专家都知道为什么?

非常感谢~~~

1 个答案:

答案 0 :(得分:0)

万一你找不到答案。我遇到了同样的问题,我的解决方案是here

更改此方法并返回true

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

希望这有帮助!!!