Android的左/右轻扫手势的WebView活动(与单击链接和垂直srolling) [英] Android left/right swipe gesture in WebView activity (with clicking on links and vertical srolling)
问题描述
解决的,解决方法是下面。
SOLVED, solution is underneath.
我想在web视图替换键导航至2滑动手势 - 左,右轻扫。该<一href="http://stackoverflow.com/questions/4139288/android-how-to-handle-right-to-left-swipe-gestures">Android:如何正确处理向左滑动手势链接真的帮了我,但我遇到的一个问题 - 执行本code我无法点击链接/或滚动的WebView活动throught加载网页后(我用的第一次从可能的答案回答)。
I want to replace buttons for navigation in webview to 2 swipe gestures - left and right swipe. This Android: How to handle right to left swipe gestures link really helped me, but I encountered problem - after implementing this code I was unable to click on links/ or scroll throught loaded webpage in webview activity(I have used 1st answer from possible answers).
后来我发现,在这个问题的另一个答案是类似的东西 - HTTP - 如何在列表视图滚动://计算器。 COM / A /三百二十七万二千四百四十九分之二千万五千四百八十一,但我不能让它运行。在logcat中我得到这个调试信息: http://s27.postimg.org/5qfipgi1v/message.png
Then I found out that in another answer in this question is something similar - how to scroll in listview - http://stackoverflow.com/a/20005481/3272449, but I wasn't able to get it running. In logcat i get this debug info: http://s27.postimg.org/5qfipgi1v/message.png
这是我OnSwipeTouchListener:
This is my OnSwipeTouchListener:
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
public boolean onTouch(final View view, final MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public GestureDetector getGestureDetector(){
return gestureDetector;
}
}
和我的WebView活动(剥离):
And my webview activity(stripped):
public class WebViewActivity extends Activity {
private WebView browser;
private OnSwipeTouchListener onSwipeTouchListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
onSwipeTouchListener = new OnSwipeTouchListener() {
public void onSwipeRight() {
Toast.makeText(WebViewActivity.this, "right", Toast.LENGTH_SHORT).show();
}
public void onSwipeLeft() {
Toast.makeText(WebViewActivity.this, "left", Toast.LENGTH_SHORT).show();
}
};
browser.setOnTouchListener(onSwipeTouchListener);
} // end of onCreate
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
onSwipeTouchListener.getGestureDetector().onTouchEvent(ev);
return super.dispatchTouchEvent(ev);
}
} // end of WebViewActivity
如何使左/右刷卡,垂直srolling和点击链接在同一时间工作?
How to make left/right swiping, vertical srolling and clicking on links working at the same time?
解决方案:同解决的问题: 一扔手势和web视图Android中
SOLUTION: Same solved question: Fling Gesture and Webview in Android
推荐答案
我不与 GestureDetector
来可靠地诊断问题非常熟悉。有一件事你可以尝试是克隆事件:
I'm not familiar enough with the GestureDetector
to reliably diagnose your problem. One thing you could try is cloning the event:
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
onSwipeTouchListener.getGestureDetector().onTouchEvent(MotionEvent.obtain(ev));
return super.dispatchTouchEvent(ev);
}
如果做不到这一点的另一种方式处理这个是使用<一个href="http://developer.android.com/reference/android/webkit/WebView.html#onOverScrolled%28int,%20int,%20boolean,%20boolean%29"相对=nofollow> onOverScrolled 代替 GestureDetector
:
@Override
protected void onOverScrolled (int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
if (scrollX > (getWidth() + THRESHOLD) && clampedX) {
onSwipeLeft();
}
if (scrollX < - THRESHOLD && clampedX) {
onSwipeRight();
}
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
}
这并不认为你的内容是不正常滚动的横盘整理,但。
This does assume that your content is not normally scrollable sideways though.
这篇关于Android的左/右轻扫手势的WebView活动(与单击链接和垂直srolling)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!