WebView常见问题汇总以及解决方案

WebView常见问题汇总以及解决方案

  1. 监听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"
    />
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值