我需要什么:我需要在ViewFlipper中使用不同的WebView,然后我想在ViewFlipper上使用gestureListening进行左右切换时的滑动操作。
我拥有的内容:我在ViewFlipper中添加了WebViews并且它们正在加载。
有什么问题:当我想向左/向右滑动时,它不起作用,WebViews也不会被更改。当我使用ImageViews测试相同的ViewFlipper和代码时,它工作正常。
ViewFlipper中的WebViews可能出现什么问题?
答案 0 :(得分:4)
只需在您的活动中添加此代码
即可@Override
public boolean dispatchTouchEvent(MotionEvent e)
{
super.dispatchTouchEvent(e);
return gestureDetector.onTouchEvent(e);
}
答案 1 :(得分:0)
我有同样的问题,上面的内容对我不起作用,因为我的活动中有其他导航按钮,而dispatchTouchEvent也覆盖了所有这些按钮。我终于崩溃并创建了一个自定义WebView并使用它而不是WebView
CustomWebView m_webview = new CustomWebView(this);
//normal webview code here
然后创建一个自定义webview来处理滑动
public class CustomWebView extends WebView
{
private final int MINIMUM_HORIZONTAL_DISTANCE = 150;
private final int MAXIMUM_VERTICAL_DISTANCE = 100;
float downXValue, downYValue;
private float lastTouchX, lastTouchY;
private boolean hasMoved = false;
public CustomWebView(Context context)
{
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent touchevent)
{
boolean consumed = super.onTouchEvent(touchevent);
if (isClickable())
{
switch (touchevent.getAction())
{
case MotionEvent.ACTION_DOWN:
lastTouchX = touchevent.getX();
lastTouchY = touchevent.getY();
downXValue = touchevent.getX();
downYValue = touchevent.getY();
hasMoved = false;
break;
case MotionEvent.ACTION_MOVE:
hasMoved = moved(touchevent);
break;
case MotionEvent.ACTION_UP:
float currentX = touchevent.getX();
float currentY = touchevent.getY();
float X_difference = Math.abs(downXValue - currentX);
float Y_difference = Math.abs(downYValue - currentY);
/**
* Next WebView
*/
if (lastTouchX < currentX && X_difference > MINIMUM_HORIZONTAL_DISTANCE && Y_difference < MAXIMUM_VERTICAL_DISTANCE)
{
//Your ViewFlipper Code here viewFlipper.showNext();
}
/**
* Previous WebView
*/
if (lastTouchX > currentX && X_difference > MINIMUM_HORIZONTAL_DISTANCE && Y_difference < MAXIMUM_VERTICAL_DISTANCE)
{
//Your ViewFlipper Code here viewFlipper.showPrevious();
}
break;
}
}
return consumed || isClickable();
}
private boolean moved(MotionEvent evt)
{
return hasMoved || Math.abs(evt.getX() - lastTouchX) > 10.0;
}
}