一:判断WebView内容高度与WebView高度
mWebViewList.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
int contentHeight = mWebViewList.getContentHeight();
int viewHeight = mWebViewList.getHeight();
if (contentHeight > viewHeight) {//内容高度>View高度
imgPullDown.setVisibility(View.VISIBLE);
} else {//内容高度<View高度
imgPullDown.setVisibility(View.GONE);
}
}
}, 500);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
二:处理WebView滑动事件判断是否滑动到底部
- 自定义WebView重写onScrollChanged方法
public class ObservableWebView extends WebView {
private OnScrolldListener mOnScrolldListener;
public ObservableWebView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public ObservableWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public ObservableWebView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
// TODO Auto-generated method stub
super.onScrollChanged(l, t, oldl, oldt);
mOnScrolldListener.Scroll(l, t, oldl, oldt);
}
public OnScrolldListener getOnScrolldListener() {
return mOnScrolldListener;
}
public void setOnScrolldListener(OnScrolldListener mOnScrolldListener) {
this.mOnScrolldListener = mOnScrolldListener;
}
/**
* Event listener.
*/
public interface OnScrolldListener {
void Scroll(int l, int t, int oldl, int oldt);
}
}
- 判断是否滑动到底部
mWebViewList.setOnScrolldListener(new OnScrolldListener() {
@Override
public void Scroll(int l, int t, int oldl, int oldt) {
// TODO Auto-generated method stub
// WebView的总高度
int webViewContentHeight = (int) (mWebViewList.getContentHeight() * mWebViewList.getScale());
// WebView的现高度
int webViewCurrentHeight = (mWebViewList.getHeight() + mWebViewList.getScrollY());
Log.i(TAG, "webViewContentHeight=" + webViewContentHeight);
Log.i(TAG, "webViewCurrentHeight=" + webViewCurrentHeight);
if ((webViewContentHeight - webViewCurrentHeight) == 0) {
System.out.println("WebView滑动到了底端");
if (imgPullDown.getVisibility() == View.VISIBLE)
imgPullDown.setVisibility(View.GONE);
} else {
if (imgPullDown.getVisibility() == View.GONE) {
imgPullDown.setVisibility(View.VISIBLE);
}
}
}
});