WebView常见问题汇总以及解决方案
- 监听WebView上下滑动
自定义WebView
import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;
/**
* 重新webview
*
* @author
*/
public class MyWebView extends WebView {
ScrollInterface web;
public MyWebView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
//Log.e("hhah",""+l+" "+t+" "+oldl+" "+oldt);
web.onSChanged(l, t, oldl, oldt);
}
public void setOnCustomScroolChangeListener(ScrollInterface t) {
this.web = t;
}
/**
* 定义滑动接口
*
* @param
*/
public interface ScrollInterface {
public void onSChanged(int l, int t, int oldl, int oldt);
}
}
这里定义了一个ScrollInterface 接口,其中l和t分别是Webview滚动后x和y方向上的坐标,oldl, oldt分别是Webview滚动前的x和y方向上的坐标。
现在一个对外提供了滚动参数的自定义webview就写好了,让我们看看如何在外部使用:
mWebView.setOnCustomScroolChangeListener(new MyWebView.ScrollInterface() {
@Override
public void onSChanged(int l, int t, int oldl, int oldt) {
//在这里我们可以根据需求,实现功能,比如监听滑动方向和是否滚动到底部
if (t - oldt > 0) {
//向下滑动要做的事情
} else if (t - oldt < 0) {
//向上滑动要做到事情
}
//WebView的总高度
float webViewContentHeight=mWebView.getContentHeight() * mWebView.getScale();
//WebView的现高度
float webViewCurrentHeight=(mWebView.getHeight() + mWebView.getScrollY());
if ((webViewContentHeight-webViewCurrentHeight) == 0) {
//滑动到底部
}
if (mWebView.getScrollY() == 0) {
//滑动到顶端
}
}
});
2 播放H5视频的时候,黑屏,只有声音没有图像
在AndroidManifest.xml中开启硬件加速
AndroidManifest.xml
android:hardwareAccelerated="true"
同时,在webview中进行layerType设置
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layerType="hardware"
android:verticalScrollbarPosition="right"
android:scrollbars="vertical"
/>