GestureScanner功能会覆盖超链接点击

时间:2017-10-04 11:51:43

标签: android webview hyperlink gesture

Hy大家, 我在WebView中遇到超链接问题。 请注意,我已经查看了解决类似问题的所有链接,但未找到任何解决完全相同问题的链接。

事实上,我有一个自定义Web视图,它在某个手势上实现了一些操作。我想这是禁用超链接的触摸。

我想知道如何保持手势并启用超链接工作并调用shouldOverrideUrlLoading

这是我的自定义网络视图:

public class newBTWebView extends WebView implements QuickAction.OnDismissListener, PopoverView.PopoverViewDelegate, GestureDetector.OnGestureListener {
public newBTWebView(Context context) {
    super(context);
    this.ctx = context;
    setup(context);
    init(context);

}

public newBTWebView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.ctx = context;
    setup(context);
    init(context);

}

public void init(Context context) {
    this.context = context;

    setInitialScale(100);
    this.getSettings().setJavaScriptEnabled(true);

    Configuration conf = getResources().getConfiguration();

    int screenLayout = 1;
    try
    {
        Field field = conf.getClass().getDeclaredField("screenLayout");
        screenLayout = field.getInt(conf);
    }
    catch (Exception e) {

    }

    loadingNewChap = false;

    screenType = screenLayout & 15;

    gestureScanner = new GestureDetector(this);
    this.setVerticalScrollBarEnabled(false);
    this.setHorizontalScrollBarEnabled(false);


    this.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();

            if(event.getAction() == MotionEvent.ACTION_DOWN)
            {
                countDown++;
            }

            if(event.getAction() == MotionEvent.ACTION_UP  && isLongPress)
            {
                 //do selection

            }
            return gestureScanner.onTouchEvent(event);
        }
    });
}

我还实现了手势的功能:onFlingDo,onSingleTapUp,onShowPress,onLongPress,onDown

这就是我在活动中调用webview的方式:

testWV = (newBTWebView) findViewById(R.id.mywebview1);
testWV.setVerticalScrollBarEnabled(false);
testWV.setHorizontalScrollBarEnabled(false);

testWV.setWebViewClient(new WebViewClient() {
     @Override
     public boolean shouldOverrideUrlLoading(WebView view, String url)
     {
           super.shouldOverrideUrlLoading(view, url);
           return false;
     }

     @Override
     public void onPageStarted(WebView view, String url, Bitmap favicon) {
     }

     @Override
     public void onPageFinished(WebView view, String url) {
     }
}

我知道如何保持手势并仍能捕获超链接点击次数吗?

2 个答案:

答案 0 :(得分:0)

请使用以下代码解决问题:

testWV .setWebChromeClient(new WebChromeClient()); 
testWV .getSettings().setJavaScriptEnabled(true); 
testWV .getSettings().setPluginsEnabled(true);
testWV .loadUrl(url); 
testWV .setWebViewClient(new WebViewClient(){

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
  view.loadUrl(url);
  return true;
}
});

答案 1 :(得分:0)

this.setOnTouchListener(new View.OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();

        if(event.getAction() == MotionEvent.ACTION_DOWN)
        {
            countDown++;
        }

        if(event.getAction() == MotionEvent.ACTION_UP  && isLongPress)
        {
             //do selection

        }
        gestureScanner.onTouchEvent(event);
        return false;
    }
});

在onTouch回调中,返回false以实现默认触摸行为。