3

My Layout xml file like this

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

and my main activity like this

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

.....

My Gesture like this:

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

there is a very strange situation. only onScroll event was triggered ~~ onFling event never was triggered ~~ i tried many ways . but it did not work ~ any experts know why ?

thanks a lot~~~

Csyr Peng
  • 31
  • 2

1 Answers1

0

Just in case you didn't find an answer. I was facing the same problem and my solution was here.

Change this method and return true

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

Hope this helps!!!

Community
  • 1
  • 1
khose
  • 743
  • 1
  • 6
  • 19