简体   繁体   English

Android Anko Kotlin - 预加载视图

[英]Android Anko Kotlin - Preload Views

I'm a bit new to Kotlin in Android, I've noticed that the Anko library supplies a nice tool for layout creation, for swiping the active view and defining the views with code.我对 Android 中的 Kotlin 有点陌生,我注意到 Anko 库提供了一个很好的布局创建工具,用于滑动活动视图并使用代码定义视图。

I'm trying to use the Anko to define different template views to show async, but before showing a new View, I would like to preload it, for example a webpage or remote image etc.我正在尝试使用 Anko 定义不同的模板视图以显示异步,但在显示新视图之前,我想预加载它,例如网页或远程图像等。

I've not been able to find any info on if this is doable or not.我无法找到有关这是否可行的任何信息。

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        MyActivityUI().load("http://some.address.ip")
        val handler = Handler()
        handler.postDelayed({
            MyActivityUI().setContentView(this)
        }, 5000)
    }
}

class MyActivityUI : AnkoComponent<MyActivity> {
    lateinit var wv: WebView

    override fun createView(ui: AnkoContext<MyActivity>) = with(ui) {
        verticalLayout {
            wv = webView {
                webViewClient = WebViewClient()
            }.lparams(width = matchParent, height = matchParent)
        }
    }
}

This is partial but shown what I intend to do, so when the 5s timer is done, the webpage is already loaded to the View, instead of it starts to load这是部分但显示了我打算做什么,所以当 5s 计时器完成时,网页已经加载到视图,而不是开始加载

Any suggestions?有什么建议么?

To answer your question exactly you could just keep a reference to the parent view and call addView to it and pass the webview in. However you might consider just setting the visibility to View.GONE/View.INVISIBLE I am not sure exactly what your goal here is.要准确回答您的问题,您可以保留对父视图的引用并调用 addView 并将 webview 传入。但是您可能会考虑将可见性设置为 View.GONE/View.INVISIBLE 我不确定您的目标到底是什么这是。 Also consider using the callback that determines the page finished loading rather than a generic 5 seconds.还可以考虑使用确定页面完成加载的回调,而不是一般的 5 秒。 Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM