我正在开发一个简单的android应用程序,里面有aRelativeLayout
和a WebView
。
我必须检测仅在屏幕左侧 20% 的区域从下到上的滑动。因此,当用户从下到上在该空间中滑动时,我必须显示一个自定义对话框。
我尝试的是:
import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
public class ActivitySwipeDetector implements View.OnTouchListener {
static final String logTag = "ActivitySwipeDetector";
private Activity activity;
static final int MIN_DISTANCE = 100;
private float downY, upY;
public ActivitySwipeDetector(Activity activity){
this.activity = activity;
}
public void onRightToLeftSwipe(){
}
public void onLeftToRightSwipe(){
}
public void onTopToBottomSwipe(){
}
public void onBottomToTopSwipe(){
System.out.println("BOTTOM TO TOP SWIPE DONE!");
}
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN: {
downY = event.getY();
return true;
}
case MotionEvent.ACTION_UP: {
upY = event.getY();
float deltaY = downY - upY;
if(Math.abs(deltaY) > MIN_DISTANCE){
if(deltaY > 0) { this.onBottomToTopSwipe(); return true; }
}
else {
return false;
}
return true;
}
}
return false;
}
}
layout = (RelativeLayout)this.findViewById(R.id.layout);
layout.setOnTouchListener(activitySwipeDetector);
但它什么也没做!
所以我尝试以这种方式创建自定义 webview:
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.webkit.WebView;
public class MyWebView extends WebView {
public MyWebView(Context context) {
super(context);
}
public MyWebView(Context context,AttributeSet set){
super(context,set);
}
@Override
public boolean onTouchEvent(MotionEvent evt) {
boolean consumed = super.onTouchEvent(evt);
if (isClickable()) {
switch (evt.getAction()) {
case MotionEvent.ACTION_DOWN:
lastTouchY = evt.getY();
downTime = evt.getEventTime();
hasMoved = false;
break;
case MotionEvent.ACTION_MOVE:
hasMoved = moved(evt);
break;
case MotionEvent.ACTION_UP:
float actualTouchY = evt.getY();
long currentTime = evt.getEventTime();
float difference = Math.abs(lastTouchY - actualTouchY);
long time = currentTime - downTime;
if ( (lastTouchY < actualTouchY) && (time < 220) && (difference > 100) ) {
System.out.println("SWIPE1");
}
if ( (lastTouchY > actualTouchY) && (time < 220) && (difference > 100) ) {
System.out.println("SWIPE2");
}
break;
}
}
return consumed || isClickable();
}
long downTime;
private float lastTouchY;
private boolean hasMoved = false;
private boolean moved(MotionEvent evt) {
return hasMoved ||
Math.abs(evt.getY() - lastTouchY) > 10.0;
}
}
但没有成功!!!有人能帮我吗??谢谢!!!!!:)