http://www.open-open.com/lib/view/open1379383341959.html
MainActivity如下:
01 | package cn.testwebview; |
02 | import android.app.Activity; |
03 | import android.graphics.Bitmap; |
04 | import android.os.Bundle; |
05 | import android.webkit.WebSettings; |
06 | import android.webkit.WebView; |
07 | import android.webkit.WebViewClient; |
08 | import cn.testwebview.TestWebView.ScrollInterface; |
14 | * 1 http://blog.csdn.net/conant1989/article/details/8124582 |
17 | public class MainActivity extends Activity { |
18 | private TestWebView mTestWebView; |
20 | protected void onCreate(Bundle savedInstanceState) { |
21 | super .onCreate(savedInstanceState); |
22 | setContentView(R.layout.main); |
29 | private void initWebView() { |
30 | mTestWebView = (TestWebView) findViewById(R.id.webView); |
31 | mTestWebView.setVerticalScrollBarEnabled( true ); |
32 | mTestWebView.setHorizontalScrollBarEnabled( false ); |
33 | mTestWebView.getSettings().setSupportZoom( true ); |
34 | mTestWebView.getSettings().setBuiltInZoomControls( true ); |
35 | mTestWebView.getSettings().setJavaScriptEnabled( true ); |
37 | mTestWebView.getSettings().setDomStorageEnabled( true ); |
38 | mTestWebView.getSettings().setPluginsEnabled( true ); |
39 | mTestWebView.requestFocus(); |
41 | mTestWebView.getSettings().setUseWideViewPort( true ); |
42 | mTestWebView.getSettings().setLoadWithOverviewMode( true ); |
43 | mTestWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); |
45 | mTestWebView.loadUrl( "http://www.ifeng.com" ); |
46 | mTestWebView.setWebViewClient( new TestWebViewClient()); |
47 | webViewScroolChangeListener(); |
52 | private void webViewScroolChangeListener() { |
53 | mTestWebView.setOnCustomScroolChangeListener( new ScrollInterface() { |
55 | public void onSChanged( int l, int t, int oldl, int oldt) { |
57 | float webViewContentHeight=mTestWebView.getContentHeight() * mTestWebView.getScale(); |
59 | float webViewCurrentHeight=(mTestWebView.getHeight() + mTestWebView.getScrollY()); |
60 | System.out.println( "webViewContentHeight=" +webViewContentHeight); |
61 | System.out.println( "webViewCurrentHeight=" +webViewCurrentHeight); |
62 | if ((webViewContentHeight-webViewCurrentHeight) == 0 ) { |
63 | System.out.println( "WebView滑动到了底端" ); |
70 | private class TestWebViewClient extends WebViewClient{ |
72 | public void onPageStarted(WebView view, String url, Bitmap favicon) { |
73 | super .onPageStarted(view, url, favicon); |
76 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
81 | public void onPageFinished(WebView view, String url) { |
82 | super .onPageFinished(view, url); |
86 | public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) { |
87 | super .onReceivedError(view, errorCode, description, failingUrl); |
TestWebView如下:
01 | package cn.testwebview; |
03 | import android.content.Context; |
04 | import android.util.AttributeSet; |
05 | import android.webkit.WebView; |
07 | public class TestWebView extends WebView { |
08 | public ScrollInterface mScrollInterface; |
10 | public TestWebView(Context context) { |
14 | public TestWebView(Context context, AttributeSet attrs, int defStyle) { |
15 | super (context, attrs, defStyle); |
18 | public TestWebView(Context context, AttributeSet attrs) { |
19 | super (context, attrs); |
23 | protected void onScrollChanged( int l, int t, int oldl, int oldt) { |
25 | super .onScrollChanged(l, t, oldl, oldt); |
27 | mScrollInterface.onSChanged(l, t, oldl, oldt); |
31 | public void setOnCustomScroolChangeListener(ScrollInterface scrollInterface) { |
33 | this .mScrollInterface = scrollInterface; |
37 | public interface ScrollInterface { |
39 | public void onSChanged( int l, int t, int oldl, int oldt); |
main.xml如下:
02 | xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | xmlns:tools = "http://schemas.android.com/tools" |
04 | android:layout_width = "match_parent" |
05 | android:layout_height = "match_parent" |
08 | < cn.testwebview.TestWebView |
09 | android:id = "@+id/webView" |
10 | android:layout_width = "fill_parent" |
11 | android:layout_height = "fill_parent" |
12 | android:text = "@string/hello_world" |
13 | android:layout_centerInParent = "true" |