Android监听WebView滑动到底部

jopen 11年前

MainActivity如下:

package cn.testwebview;  import android.app.Activity;  import android.graphics.Bitmap;  import android.os.Bundle;  import android.webkit.WebSettings;  import android.webkit.WebView;  import android.webkit.WebViewClient;  import cn.testwebview.TestWebView.ScrollInterface;  /**   * Demo描述:   * 监听WebView滑动到底部   *    * 参考资料:   * 1 http://blog.csdn.net/conant1989/article/details/8124582   *   Thank you very much   */  public class MainActivity extends Activity {      private TestWebView mTestWebView;   @Override   protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    initWebView();   }            //设置WebView   private void initWebView() {    mTestWebView = (TestWebView) findViewById(R.id.webView);    mTestWebView.setVerticalScrollBarEnabled(true);    mTestWebView.setHorizontalScrollBarEnabled(false);    mTestWebView.getSettings().setSupportZoom(true);    mTestWebView.getSettings().setBuiltInZoomControls(true);    mTestWebView.getSettings().setJavaScriptEnabled(true);        mTestWebView.getSettings().setDomStorageEnabled(true);    mTestWebView.getSettings().setPluginsEnabled(true);    mTestWebView.requestFocus();        mTestWebView.getSettings().setUseWideViewPort(true);    mTestWebView.getSettings().setLoadWithOverviewMode(true);    mTestWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);        mTestWebView.loadUrl("http://www.ifeng.com");    mTestWebView.setWebViewClient(new TestWebViewClient());    webViewScroolChangeListener();       }      //核心代码   private void webViewScroolChangeListener() {    mTestWebView.setOnCustomScroolChangeListener(new ScrollInterface() {     @Override     public void onSChanged(int l, int t, int oldl, int oldt) {      //WebView的总高度      float webViewContentHeight=mTestWebView.getContentHeight() * mTestWebView.getScale();      //WebView的现高度      float webViewCurrentHeight=(mTestWebView.getHeight() + mTestWebView.getScrollY());      System.out.println("webViewContentHeight="+webViewContentHeight);      System.out.println("webViewCurrentHeight="+webViewCurrentHeight);      if ((webViewContentHeight-webViewCurrentHeight) == 0) {       System.out.println("WebView滑动到了底端");      }     }    });   }           private class TestWebViewClient extends WebViewClient{    @Override    public void onPageStarted(WebView view, String url, Bitmap favicon) {     super.onPageStarted(view, url, favicon);    }    @Override    public boolean shouldOverrideUrlLoading(WebView view, String url) {     view.loadUrl(url);     return true;    }    @Override    public void onPageFinished(WebView view, String url) {     super.onPageFinished(view, url);         }    @Override    public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {     super.onReceivedError(view, errorCode, description, failingUrl);    }   }       }


TestWebView如下:

package cn.testwebview;    import android.content.Context;  import android.util.AttributeSet;  import android.webkit.WebView;    public class TestWebView extends WebView {   public ScrollInterface mScrollInterface;     public TestWebView(Context context) {    super(context);   }     public TestWebView(Context context, AttributeSet attrs, int defStyle) {    super(context, attrs, defStyle);   }     public TestWebView(Context context, AttributeSet attrs) {    super(context, attrs);   }     @Override   protected void onScrollChanged(int l, int t, int oldl, int oldt) {      super.onScrollChanged(l, t, oldl, oldt);      mScrollInterface.onSChanged(l, t, oldl, oldt);     }     public void setOnCustomScroolChangeListener(ScrollInterface scrollInterface) {      this.mScrollInterface = scrollInterface;     }     public interface ScrollInterface {      public void onSChanged(int l, int t, int oldl, int oldt);     }    }


 

main.xml如下:

<RelativeLayout       xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"    >        <cn.testwebview.TestWebView          android:id="@+id/webView"          android:layout_width="fill_parent"          android:layout_height="fill_parent"          android:text="@string/hello_world"          android:layout_centerInParent="true"      />    </RelativeLayout>