我正在android应用程序中进行Web视图,并希望实现滑动以刷新手势。但由于一个错误而无法通过。 swipe.setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener()) 应该向该函数传递什么参数?
这是我的mainactivity.kt文件
var mWebView : WebView? = null
abstract class MainActivity : AppCompatActivity() {
lateinit var swipe:SwipeRefreshLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
swipe = findViewById(R.id.swipe) as SwipeRefreshLayout
**swipe.setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener())**
run {
val onRefresh:Unit
run({ Loadweb() })
}
}
fun Loadweb() {
mWebView = findViewById<View>(R.id.webView) as WebView
mWebView!!.webViewClient = object : WebViewClient () {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
view?.loadUrl(url)
return true
}
}
val webSettings = mWebView!!.getSettings()
webSettings.setJavaScriptEnabled(true)
webSettings.setUseWideViewPort(true)
mWebView!!.loadUrl("http://allnumber.info/")
}
override fun onBackPressed() {
if (mWebView!!.canGoBack()){
mWebView!!.goBack()
}
else {
super.onBackPressed()
}
}}
这是错误消息
没有传递参数'function'的值
答案 0 :(得分:1)
尝试此代码
private lateinit var mHandler: Handler
private lateinit var mRunnable:Runnable
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize the handler instance
mHandler = Handler()
swipe = findViewById(R.id.swipe) as SwipeRefreshLayout
swipe.setOnRefreshListener{
mRunnable = Runnable {
Loadweb()
swipe_refresh_layout.isRefreshing = false
}
// Execute the task after specified time
mHandler.postDelayed(
mRunnable,
(randomInRange(1,5)*1000).toLong() // Delay 1 to 5 seconds
)
}
}
}
答案 1 :(得分:0)
我认为你应该通过
(object:SwipeRefreshLayout.OnRefreshListener(){
//override methods
})