当前位置: 代码迷 >> Web前端 >> webview 切换及扩充
  详细解决方案

webview 切换及扩充

热度:798   发布时间:2012-09-21 15:47:26.0
webview 切换及扩展

MyWebView[] webview =new MyWebView[2];?
? ? flipper
= (ViewFlipper) findViewById(R.id.ViewFlipper);?
?
?webview
[i] = new MyWebView(this);?
?webview
[i].setWebViewClient(new HelloWebViewClient());?
?webview
[i].getSettings().setJavaScriptEnabled(false);?
?webview
[i].setInitialScale(60); ?
?webview
[i].getSettings().setBuiltInZoomControls(true);?
?
? ? flipper
.addView(webview[0]);?
? ? flipper
.addView(webview[1]);?

public class MyWebView extends WebView { 
  public MyWebView(Context context) { 
   super(context); 
  } 
 
 
 
@Override  
     public boolean onTouchEvent(MotionEvent evt) {    
 
         boolean consumed = super.onTouchEvent(evt);  
     if (isClickable()) {  
         switch (evt.getAction()) {  
     case MotionEvent.ACTION_DOWN:  
         lastTouchX = evt.getX();  
         lastTouchY = evt.getY(); 
         downXValue = evt.getX(); 
         downTime = evt.getEventTime(); 
         hasMoved = false;  
         break;  
     case MotionEvent.ACTION_MOVE:  
         hasMoved = moved(evt);  
         break;  
     case MotionEvent.ACTION_UP:  
        float currentX = evt.getX(); 
           long currentTime = evt.getEventTime(); 
           float difference = Math.abs(downXValue - currentX); 
           long time = currentTime - downTime; 
 
           Log.i("Touch Event:",  "Distance: " + difference + "px Time: " + time + "ms"); 
 
           if ( (downXValue < currentX) && (time < 220) && (difference > 100) ) { 
               go_back(); 
           } 
 
 
 
             if ( (downXValue > currentX) && (time < 220) && (difference > 100) ) { 
                   go_forward(); 
 
 
                  } 
 
                 //if (!moved(evt)) performClick();  
                 break;  
             }  
         }  
         return consumed || isClickable();  
     }  
  float downXValue; 
  long downTime; 
     private float lastTouchX, lastTouchY;  
     private boolean hasMoved = false;  
     private boolean moved(MotionEvent evt) {  
         return hasMoved ||  
             Math.abs(evt.getX() - lastTouchX) > 10.0 ||  
             Math.abs(evt.getY() - lastTouchY) > 10.0;  
     } 
 
 } 

?

1 楼 maniunet 2011-07-25  
这样webview左右滑动有了,但是如果webview内容较长的时候,上下的手势不能滚动查看内容了,请问如何能既可以左右滑动,但是不影响webview手势的上下滚动