2

我正在使用ViewPager Extension使我的应用程序具有标签功能。我的应用中有两个选项卡,我们在这里命名为 tabA 和 tabB。tabA 包含一个 webview 组件,而 tabB 包含一个 listview。

现在,我遇到了一个问题:一旦我尝试从 tabA 滚动到 tabB,tabA 中的页面会在更改为 tabB 时(或之前)更改。换句话说,当我尝试从 tabA 滚动到 tabB 时,我的要求是不要触发 tabA 中的点击事件。

有没有人对这个问题有任何想法或经验,非常感谢!

我曾尝试使用 GestureDetector 来检测手势,但它对我不起作用。

final GestureDetector dector = new GestureDetector(new OnGestureListener(){...}
webView.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        return dector.onTouchEvent(event);
    }
});
4

3 回答 3

0

我有同样的问题。我已经搜索了几个需要在 java 中编码的想法,但我认为这不是好的、简单、干净的方式。然后我找到了一个非常简单和干净的解决方案。你只需要使用android:descendantFocusability="blocksDescendants"这行代码在 webView 的父 XML 中使用这行代码。而已

于 2015-01-13T04:24:44.250 回答
0

你能做的是

  1. 然后检测滑动并设置一个标志

    public interface TouchDelegate {
       public void gestureHandler(String msg, float downX, float downY);
    
       public boolean notASwipe(View v, MotionEvent event);
    }
    
    public class SwipeDetector implements View.OnTouchListener {
    
       private TouchDelegate mView;
       static final int MIN_DISTANCE = 50;
       private float downX, downY, upX, upY;
       public SwipeDetector(TouchDelegate view) {
          this.mView = view;
       }
    
       public void onRightToLeftSwipe() {
          mView.gestureHandler("R2L", downX, downY);
       }
    
       public void onLeftToRightSwipe() {
          mView.gestureHandler("L2R", downX, downY);
       }
    
       public void onTopToBottomSwipe() {
          mView.gestureHandler("T2B", downX, downY);
       }
    
       public void onBottomToTopSwipe() {
          mView.gestureHandler("B2T", downX, downY);
       }
    
       public boolean onTouch(View v, MotionEvent event) {
       switch (event.getAction()) {
       case MotionEvent.ACTION_DOWN: {
           downX = event.getX();
           downY = event.getY();
           return true;
       }
       case MotionEvent.ACTION_UP: {
           upX = event.getX();
           upY = event.getY();
    
           float deltaX = downX - upX;
           float deltaY = downY - upY;
    
           // swipe horizontal?
           if (Math.abs(deltaX) > MIN_DISTANCE) {
               // left or right
               if (deltaX < 0) {
                   this.onLeftToRightSwipe();
                   return true;
               }
               if (deltaX > 0) {
                   this.onRightToLeftSwipe();
                   return true;
               }
           } else {
               // swipe vertical?
               if (Math.abs(deltaY) > MIN_DISTANCE) {
                   // top or down
                   if (deltaY < 0) {
                       this.onTopToBottomSwipe();
                       return true;
                   }
                   if (deltaY > 0) {
                       this.onBottomToTopSwipe();
                       return true;
                   }
               } else {
                   Log.d(getClass().getSimpleName(),
                           "SwipeDetector: onTouch: Swipe was only x: " + Math.abs(deltaX)
                                + " long, need at least " + MIN_DISTANCE);
                Log.d(getClass().getSimpleName(),
                        "SwipeDetector: onTouch: Swipe was only y: "
                                + Math.abs(deltaY)
                                + " long, need at least " + MIN_DISTANCE);
                return mView.notASwipe(v, event); // We don't consume the
                                                    // event
            }
        }
        return true;
    }
    }
    return false;
    }
    }
    
  2. 覆盖 shouldOverrideUrlLoading 以禁用 webview 上的所有链接。这是禁用 webview 上所有链接的代码:

    public class MyWebViewClient extends WebViewClient {
     public boolean shuldOverrideKeyEvent (WebView view, KeyEvent event) {
        if(swipe)              
           return true;
         return false;
     }
     public boolean shouldOverrideUrlLoading (WebView view, String url) {
       if (!swipe) {
            view.loadUrl(url);
       }
       return true;
     }
    }
    
于 2013-04-25T02:27:53.833 回答
0

子类化WebView,然后像这样覆盖该onTouchEvent方法:

@Override
public boolean onTouchEvent(MotionEvent event)
{
    // If this web view gets sent a 'CANCEL' event, it means the 
    // parent view has intercepted the event (for scrolling / swiping),
    // so we want to catch it, otherwise it propagates into the HTML 
    // and triggers the click
    int action = MotionEventCompat.getActionMasked(event);
    if(action == MotionEvent.ACTION_CANCEL)
    {
        return true;
    }
    return super.onTouchEvent(event);
}

应该发生的是,当有适当的滑动时,封闭的 ViewPager 已经拦截了触摸事件,这会将ACTION_CANCEL事件发送到 WebView。通过在方法中捕获该ACTION_CANCEL事件onTouchEvent,它可以防止该事件进入 WebView 中的 HTML。

于 2014-02-25T06:02:50.840 回答