要导入的依赖:
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()
}
}
}