如何在WebView中实现滑动操作

时间:2011-10-18 18:28:46

标签: android webview viewflipper swipe

enter code here我是Android的新手,我正在尝试使用不同的网址为每个视图创建网络视图,我想添加滑动操作以翻转视图。但我没有获得任何视图Launch.I我正在使用ViewFlipper浏览视图。

    My main.xml file is:
    <?xml version="1.0" encoding="utf-8"?>
    <ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ViewFlipper" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <WebView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/webview1"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent" />
        <WebView xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/webview2" 
             android:layout_width="fill_parent"
             android:layout_height="fill_parent" />
      </ViewFlipper>

我的Java文件如下:

 public class SwipeSearchActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    MyWebView[] webview =new MyWebView[2];
    ViewFlipper flipper = (ViewFlipper) findViewById(R.id.ViewFlipper);
    webview[0] = new MyWebView(this);
    webview[0].setWebViewClient(new WebViewClient());
    webview[0].getSettings().setJavaScriptEnabled(false);
    webview[0].setInitialScale(60); 
    webview[0].getSettings().setBuiltInZoomControls(true);
    webview[0].loadUrl("http://www.google.com");
    webview[1] = new MyWebView(this);
    webview[1].setWebViewClient(new WebViewClient());
    webview[1].getSettings().setJavaScriptEnabled(false);
    webview[1].setInitialScale(60); 
    webview[1].getSettings().setBuiltInZoomControls(true);
    webview[1].loadUrl("http://www.bing.com");
    flipper.addView(webview[0]);
    flipper.addView(webview[1]);
}

} 我的自定义WebView类是:

  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) ) {
               goBack();
           }



             if ( (downXValue > currentX) && (time < 220) && (difference > 100) ) {
                   goForward();


                  }

                 //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; 
     }
}

请帮我弄清楚这里有什么问题..

0 个答案:

没有答案