Tôi đang phát triển một ứng dụng Android đơn giản với một số RelativeLayout
và một bên là WebView
.Cách phát hiện cử chỉ vuốt trên chế độ xem web
Tôi phải phát hiện swipe từ dưới lên trên chỉ được thực hiện trong 20% phần bên trái của màn hình. Vì vậy, khi người dùng vuốt trong không gian đó từ dưới lên trên, tôi phải hiển thị hộp thoại tùy chỉnh.
gì tôi cố gắng là:
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);
Nhưng nó không làm gì cả!
Vì vậy, tôi cố gắng tạo ra một webview tùy chỉnh theo cách này:
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;
}
}
nhưng không thành công !!! Ai đó có thể giúp tôi?? Cảm ơn!!!!! :)
chế độ xem web của tôi không có phương thức setGestureDetector ... Tôi đang sử dụng Android 4.0 ... – JackTurky
xin lỗi đã quên thêm chế độ xem web tùy chỉnh..bây giờ – Nermeen
tuyệt vời !!!! tôi loại bỏ tất cả ngoại trừ nếu dưới cùng của đầu trang (những gì tôi cần) và tôi thấy rằng nếu tôi rời khỏi webView.getScrollY()> = webView.getScale() * (webView.getContentHeight() - webView.getHeight() nó cho tôi ngoại trừ nó hoạt động tuyệt vời !!!!!!! tôi có thể cho tôi biết làm thế nào để nhận ra rằng cử chỉ chỉ trên một phần của màn hình? cảm ơn! – JackTurky