zoukankan      html  css  js  c++  java
  • Android监听WebView滑动到底部

    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>


     

  • 相关阅读:
    windows 共享文件夹 给 mac
    给mac配置adb 路径
    关于android 加载https网页的问题
    http tcp udp ip 间的关系
    手机服务器微架构设计和实现专题
    添加ssh key
    本人对于线程池的理解和实践
    使用Android Butterknife
    记一次失败的笔试(华为研发工程师-汽水瓶笔试题)
    简易坦克大战python版
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3161504.html
Copyright © 2011-2022 走看看