滑动监听的话是需要在WebView基础之上在加强一下,因为在WebView没有直接监听滑动的方法,看WebView的源码则会发现有一个
protected void onScrollChanged(int l, int t, int oldl, int oldt) ;
这个方法。是受到保护的所以我们无法直接使用,所以我们写一个加强的WebView,利用接口回调。
package com.song.webviewtest;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.webkit.WebView;
public class ScrollWebView extends WebView {
public OnScrollChangeListener listener;
public ScrollWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ScrollWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScrollWebView(Context context) {
super(context); }
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
float webcontent = getContentHeight() * getScale();// webview的高度
float webnow = getHeight() + getScrollY();// 当前webview的高度
Log.i("TAG1", "webview.getScrollY()====>>" + getScrollY());
if (Math.abs(webcontent - webnow) < 1) {
// 已经处于底端
// Log.i("TAG1", "已经处于底端");
listener.onPageEnd(l, t, oldl, oldt);
} else if (getScrollY() == 0) {
// Log.i("TAG1", "已经处于顶端");
listener.onPageTop(l, t, oldl, oldt);
} else {
listener.onScrollChanged(l, t, oldl, oldt); } }
public void setOnScrollChangeListener(OnScrollChangeListener listener) {
this.listener = listener; }
public interface OnScrollChangeListener {
public void onPageEnd(int l, int t, int oldl, int oldt);
public void onPageTop(int l, int t, int oldl, int oldt);
public void onScrollChanged(int l, int t, int oldl, int oldt); }}
然后,利用这个加强的WebView就可以监听了
package com.song.webviewtest;
import java.util.HashMap;
import java.util.Map;import android.app.Activity;
import android.os.Bundle;import android.os.Handler;
import android.os.Message;
import android.webkit.WebSettings;
import com.song.webviewtest.ScrollWebView.OnScrollChangeListener;
public class WebViewActivity extends Activity {
private String url = "http://192.168.30.199:8080/song/test.html";
//执行动作
public static final int SELECT_IMAGE = 0;
// 打开图库
public static final int OPEN_PAGE = 1;
// 跳转其他特定页面
public static final int CLOSE_OR_BACK = 2;
// 关闭或
private ScrollWebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webviw);
getIntentDatas();
initView();
}
private void getIntentDatas() {
// TODO Auto-generated method stub
url = getIntent().getStringExtra("url");
}
private void initView() {
mWebView = (ScrollWebView) findViewById(R.id.webview);
//或的WebView的Setting
WebSettings settings = mWebView.getSettings();
//设置支持js,看方法名字就知道啥意思
settings.setJavaScriptEnabled(true);
//mWebView.addJavascriptInterface(new JavaScriptInterface(handler), "Android");
mWebView.setOnScrollChangeListener(new OnScrollChangeListener() {
@Override
public void onScrollChanged(int l, int t, int oldl, int oldt) {
//滑动中
}
@Override
public void onPageTop(int l, int t, int oldl, int oldt) {
//滑动到顶部
}
@Override
public void onPageEnd(int l, int t, int oldl, int oldt) {
//滑动到底部
}
});
//加载网页路径
mWebView.loadUrl(url);
} }