AgentWeb_SmrtRefresh联动使用

要导入的依赖:


smartrefresh
//下拉刷新
    implementation  'com.scwang.smart:refresh-layout-kernel:2.0.1'
    implementation  'com.scwang.smart:refresh-header-classics:2.0.1'
    implementation  'com.scwang.smart:refresh-footer-classics:2.0.1'
    implementation  'com.scwang.smart:refresh-header-material:2.0.1'


agentweb
    implementation 'com.just.agentweb:agentweb-androidx:4.1.4'
    implementation 'com.just.agentweb:filechooser-androidx:4.1.4'
    implementation 'com.download.library:downloader-androidx:4.1.4'

xml文件

    
<com.scwang.smart.refresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/refreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/linearlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"></LinearLayout>

    </com.scwang.smart.refresh.layout.SmartRefreshLayout>

//初始化 agentweb

   mAgentWeb  = AgentWeb.with(this) //传入Activity
                .setAgentWebParent(linearlayout, LinearLayout.LayoutParams(-1, -1)) //传入AgentWeb 的父控件 ,如果父控件为 RelativeLayout , 那么第二参数需要传入 RelativeLayout.LayoutParams
                .useDefaultIndicator() // 使用默认进度条
                .createAgentWeb() //
                .ready()
                .go("https://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=com.scwang.smart")


调用js

注意要延时加载 不然调用不了


        button.setOnClickListener {  //    调用js
            val data = "我是迪迦"
            mAgentWeb.jsAccessEntrace.quickCallJs("callByAndroid", data)
            Snackbar.make(it, data, Snackbar.LENGTH_LONG).setAction("Action", null).show() //底部消息弹出框
        }

//注入对象 js调用Android


     //注入对象          js调用Android
        mAgentWeb.jsInterfaceHolder.addJavaObject("android", AndroidInterface())  //为js调用提供接口

接口类
class AndroidInterface{
        @JavascriptInterface
        fun startActivity(id: String){
            Toast.makeText(this@AgentWeb_SmrtRefresh, "js调用android $id", Toast.LENGTH_SHORT).show()
        }
    }


联动SmrtRefresh

        refreshLayout.setRefreshHeader(MaterialHeader(this)) //谷歌样式

// refreshLayout.setRefreshHeader(ClassicsHeader(this))
refreshLayout.setRefreshFooter(ClassicsFooter(this))

    refreshLayout.setOnRefreshListener(object : OnRefreshListener {
        override fun onRefresh(refreshLayout: RefreshLayout) {
            refreshLayout.finishRefresh(/*,false*/);//传入false表示刷新失败
            mAgentWeb.urlLoader.reload()//重新加载AgentWeb
        }
    })

    refreshLayout.setOnLoadMoreListener {
        it.finishLoadMore(2000)//传入false表示加载失败
    }

    refreshLayout.autoRefresh()   //自动刷新

完整代码

package com.example.myapplication

import android.os.Bundle
import android.webkit.JavascriptInterface
import android.widget.LinearLayout
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.snackbar.Snackbar
import com.just.agentweb.AgentWeb
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import kotlinx.android.synthetic.main.activity_main.*


class AgentWeb_SmrtRefresh : AppCompatActivity() {
    lateinit var mAgentWeb: AgentWeb
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //初始化 agentweb
         mAgentWeb  = AgentWeb.with(this) //传入Activity
                .setAgentWebParent(linearlayout, LinearLayout.LayoutParams(-1, -1)) //传入AgentWeb 的父控件 ,如果父控件为 RelativeLayout , 那么第二参数需要传入 RelativeLayout.LayoutParams
                .useDefaultIndicator() // 使用默认进度条
                .createAgentWeb() //
                .ready()
                .go("https://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=com.scwang.smart")



        button9.setOnClickListener {  //    调用js
            val data = "我是迪迦"
            mAgentWeb.jsAccessEntrace.quickCallJs("callByAndroid", data)
            Snackbar.make(it, data, Snackbar.LENGTH_LONG).setAction("Action", null).show() //底部消息弹出框
        }

        //注入对象          js调用Android
        mAgentWeb.jsInterfaceHolder.addJavaObject("android", AndroidInterface())  //为js调用提供接口

        initRefresh()



    }

    private fun initRefresh() {
        refreshLayout.setRefreshHeader(MaterialHeader(this)) //谷歌样式
//        refreshLayout.setRefreshHeader(ClassicsHeader(this))
        refreshLayout.setRefreshFooter(ClassicsFooter(this))

        refreshLayout.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                refreshLayout.finishRefresh(/*,false*/);//传入false表示刷新失败
                mAgentWeb.urlLoader.reload()//AgentWeb
            }
        })

        refreshLayout.setOnLoadMoreListener {
            it.finishLoadMore(2000)//传入false表示加载失败
        }

        refreshLayout.autoRefresh()   //自动刷新
    }

    inner class AndroidInterface{
        @JavascriptInterface
        fun startActivity(id: String){
            Toast.makeText(this@AgentWeb_SmrtRefresh, "js调用android $id", Toast.LENGTH_SHORT).show()
        }
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值