Android笔记(十九):WebView浏览器多标签页(多窗口)功能新的实现

效果图

基于android实现浏览器多标签页

背景

之前写过的文章——WebView+Fragment+ViewPager构建浏览器多标签页
虽然实现了多标签页的功能,但是有个弊端就是随着创建的标签页数量增加,viewpager的滑动会越来越卡。之前一直没有时间优化,最近尝试着优化了一下,对Dainty浏览器的多标签页功能抽取出来,进行改造。

思路

在这里插入图片描述
WebTabManager用于管理tab页面对象

class WebTabManager private constructor(){

    private object Holder{
        val instance = WebTabManager()
    }

    companion object Instance {
        var increaseKey = 0
        @JvmStatic
        fun getInstance(): WebTabManager{
            return Holder.instance
        }
    }

    private val cacheWebTab = LinkedList<WebBean>()

    fun divideAllWebView(){
        LogUtil.d("divideAllWebView: ${cacheWebTab.size}")
        for (wb in cacheWebTab){
            wb.webView.parent?.let {
                (it as ViewGroup).removeAllViews()
            }
        }
    }

    fun getCacheWebTab(): List<WebBean>{
        return cacheWebTab
    }

    fun addNewTab(): WebBean {
        val webView = CommonWebView(MutableContextWrapper(Dainty2Application.app))
        val wb = WebBean(increaseKey++, webView, "首页", ContextCompat.getDrawable(Dainty2Application.app, R.drawable.web_tab_icon_home), ContextCompat.getDrawable(Dainty2Application.app, R.drawable.ic_launcher_foreground))
        cacheWebTab.add(wb)
        return wb
    }

    fun joinWebTabToLast(key: Int){
        for (wb in cacheWebTab){
            if (wb.id == key){
                cacheWebTab.remove(wb)
                cacheWebTab.addLast(wb)
                break
            }
        }
    }

    fun updateLastTabTitle(tabTitle: String){
        val wb = cacheWebTab.last
        wb.tabTitle = tabTitle
    }

    fun updateLastTabIcon(tabIcon: Drawable){
        val wb = cacheWebTab.last
        wb.tabIcon = tabIcon
    }
}

tab页面对象,保存了webview对象,网页icon和标题,网页截图

data class WebBean(val id: Int, val webView: CommonWebView, var tabTitle: String,
                   var tabIcon: Drawable?, var picture: Drawable?)

用于获取网页截图

    fun catchWebView(context: Context){
        val capture = WebTabManager.getInstance().getCacheWebTab().last().webView
        capture.isDrawingCacheEnabled = true
        val bitmap = capture.drawingCache
        WebTabManager.getInstance().getCacheWebTab().last().picture = BitmapDrawable(context.resources, bitmap).current
    }

具体可参考:Dainty2

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萌面小侠Plus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值