我的布局 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 事件从未触发过 ~~ 我尝试了很多方法。但它没有工作〜任何专家知道为什么?
非常感谢~~~