我正在使用ViewPager Extension让我的应用具有标签功能。我的应用程序中有两个选项卡,我们在这里命名为tabA和tabB。 tabA包含webview组件,tabB包含listview。
现在,我遇到了一个问题: 一旦我尝试从tabA滚动到tabB,tabA中的页面在更改为tabB时(或之前)更改。换句话说,当我尝试从tabA滚动到tabB时,我的要求是不要触发tabA中的click事件。
有没有人对此问题有任何想法或经验,非常感谢!
我曾尝试使用GestureDetector检测手势,但这对我不起作用。
final GestureDetector dector = new GestureDetector(new OnGestureListener(){...}
webView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return dector.onTouchEvent(event);
}
});
答案 0 :(得分:0)
你能做的就是那个
检测滑动并设置标志,然后
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;
}
}
覆盖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;
}
}
答案 1 :(得分: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。
答案 2 :(得分:0)
我有同样的问题。我已经搜索了几个需要在java编码的想法,但我认为这不是好的,简单的,干净的方式。然后我找到了一个非常简单和干净的解决方案。您只需要使用此行android:descendantFocusability="blocksDescendants"
在webView的XML父级中使用此行代码。多数民众赞成