在WebView中使用Android singleTap / OnClick

我正在加载Web视图与Htmlstring,我想显示导航button,当用户点击Web视图时,我试着用onTouch侦听器,触摸事件引发滚动和点击,但我想要捕捉单击/ clickEvent,不滚动事件触及…,我实现了WebView和LinearLayout的SetOnClickListener,他们没有一个不为我工作任何关于这个的帮助

WebView不支持OnClickListener 。 而且它也会消耗触摸事件,即使网页没有任何事情发生,因此祖先视图(如LinearLayout )没有任何机会产生OnClick事件。 这是非常不幸的。

作为一种解决方法,我扩展了一个RelativeLayout并把我的WebView放在里面。 在RelativeLayout我覆盖了onInterceptTouchEvent并正在寻找轻敲事件。 如果检测到轻OnClickListener则使用performClick()调用RelativeLayoutOnClickListener

 public class TapAwareRelativeLayout extends RelativeLayout { private final float MOVE_THRESHOLD_DP = 20 * getResources().getDisplayMetrics().density; private boolean mMoveOccured; private float mDownPosX; private float mDownPosY; public TapAwareRelativeLayout(Context context) { super(context); } public TapAwareRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { final int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: mMoveOccured = false; mDownPosX = ev.getX(); mDownPosY = ev.getY(); break; case MotionEvent.ACTION_UP: if (!mMoveOccured) { // TAP occured performClick(); } break; case MotionEvent.ACTION_MOVE: if (Math.abs(ev.getX() - mDownPosX) > MOVE_THRESHOLD_DP || Math.abs(ev.getY() - mDownPosY) > MOVE_THRESHOLD_DP) { mMoveOccured = true; } break; } return super.onInterceptTouchEvent(ev); } 

}

从这个页面: http : //www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/

触摸事件似乎必须被适当地困住。 看一下这个:

 @Override public boolean dispatchTouchEvent(MotionEvent e){ super.dispatchTouchEvent(e); return mGestureDetector.onTouchEvent(e); } 

我修改了Zsolt Safrany的回答,并将他的onInterceptTouchEvent的内容放到了webview的onTouch方法中,并且工作正常。

 webview.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Log.v(TAG,"Got a touch event in the web view!"); final int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: mMoveOccured = false; mDownPosX = ev.getX(); mDownPosY = ev.getY(); break; case MotionEvent.ACTION_UP: if (!mMoveOccured) { //click operation is here } break; case MotionEvent.ACTION_MOVE: if (Math.abs(ev.getX() - mDownPosX) > MOVE_THRESHOLD_DP || Math.abs(ev.getY() - mDownPosY) > MOVE_THRESHOLD_DP) { mMoveOccured = true; } break; } return false; } }); 

工作代码::

 final Boolean[] mMoveOccured = new Boolean[1]; final float[] mDownPosX = new float[1]; final float[] mDownPosY = new float[1]; final float MOVE_THRESHOLD_DP = 20 * getResources().getDisplayMetrics().density; userPic.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { final int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: mMoveOccured[0] = false; mDownPosX[0] = event.getX(); mDownPosY[0] = event.getY(); break; case MotionEvent.ACTION_UP: if (!mMoveOccured[0]) { Toast.makeText(v.getContext(), "Webview pressed", Toast.LENGTH_SHORT).show(); } break; case MotionEvent.ACTION_MOVE: if (Math.abs(event.getX() - mDownPosX[0]) > MOVE_THRESHOLD_DP || Math.abs(event.getY() - mDownPosY[0]) > MOVE_THRESHOLD_DP) { mMoveOccured[0] = true; } break; } return false; } });