Jam*_*s07 5 android webview gestures swipe
已解决,解决方案在下面.
我想将webview中的导航按钮替换为2次滑动手势 - 左右滑动.这个Android:如何处理从右向左滑动手势链接真的帮助了我,但我遇到了问题 - 实现此代码后我无法点击链接/或滚动浏览webview活动中的加载网页(我已经使用了可能的答案的第一个答案).
然后我发现在这个问题的另一个答案是类似的 - 如何在listview中滚动 - /sf/answers/1400383701/,但我无法让它运行.在logcat中我得到这个调试信息:http: //s27.postimg.org/5qfipgi1v/message.png
这是我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
我的webview活动(剥离):
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
Run Code Online (Sandbox Code Playgroud)
如何进行左/右滑动,垂直滚动和点击链接同时工作?
解决方案:同样解决的问题: Android中的Fling Gesture和Webview
我不太熟悉,无法GestureDetector可靠地诊断您的问题。您可以尝试的一件事是克隆事件:
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
onSwipeTouchListener.getGestureDetector().onTouchEvent(MotionEvent.obtain(ev));
return super.dispatchTouchEvent(ev);
}
Run Code Online (Sandbox Code Playgroud)
如果失败,另一种解决方法是使用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);
}
Run Code Online (Sandbox Code Playgroud)
但这确实假设您的内容通常不能横向滚动。
| 归档时间: |
|
| 查看次数: |
8012 次 |
| 最近记录: |