简体   繁体   English

ProgressBar 不会在 Android Studio 中从代码中可见

[英]ProgressBar wont be visible in Android Studio from code

So I have made an app, and I'm trying to show the progress bar at the time when a button is clicked.所以我做了一个应用程序,我试图在单击按钮时显示进度条。

It shows only if I put the View.VISIBLE outside the handler.它仅在我将View.VISIBLE放在处理程序之外时才显示。

Kotlin code:科特林代码:

button.setOnClickListener {

progressBar.visibility = View.VISIBLE
    runBlocking {

        try {
            //HTTP request removed because of confidentiality, assume I'm setting delay

        } catch (e: Exception) {
            Log.e(tag, "Error: " + e.message)
            Toast.makeText(this@MainActivity, "Could not find page", Toast.LENGTH_LONG)
                .show()
        }

    }
    progressBar.visibility = View.GONE
}

Button XML:按钮 XML:

<ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:alpha="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility= "invisible"/>

I've been searching for hours now and couldn't find something relevant to my case.我已经搜索了几个小时,但找不到与我的案例相关的内容。

Output wanted: Get the progress bar at the time the button is clicked.想要的输出:在单击按钮时获取进度条。

Executing a command like progressBar.visibility = View.VISIBLE may take a few nanoseconds(144100 approx).执行像progressBar.visibility = View.VISIBLE这样的命令可能需要几纳秒(大约 144100)。

The main thread might ask some other threads to perform some tasks and instead of waiting for the response of those threads, it resumes its normal execution(next lines of code).主线程可能会要求一些其他线程执行一些任务,而不是等待这些线程的响应,而是恢复其正常执行(下一行代码)。 Your next line of code(runBlocking: Runs a new coroutine and blocks the current thread interruptibly until its completion.) blocks the current until the coroutine started from the runBlocking method is finished.你的下一行代码(runBlocking:运行一个新的协程并中断地阻塞当前线程直到它完成。)阻塞当前直到从 runBlocking 方法启动的协程完成。 This simply means that the Main thread can't execute other commands or process responses received from the other threads until the coroutine is finished.这只是意味着在协程完成之前,主线程无法执行其他命令或处理从其他线程接收到的响应。

Once the coroutine is finished.一旦协程完成。 Main thread resumes its execution, showing and hiding progress bar simultaneously, which is why you don't see the progress bar spinning.主线程恢复执行,同时显示和隐藏进度条,这就是为什么您看不到进度条旋转的原因。

Main Thread should only be used for UI operations.主线程应该只用于 UI 操作。 Instead of blocking your main thread, you should perform the network operations in coroutine by switching to IO context.您应该通过切换到 IO 上下文在协程中执行网络操作,而不是阻塞主线程。

Check here on types of Dispatchers.在此处查看 Dispatcher 的类型。 https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb

bt.setOnClickListener {
    progressBar.visibility = View.VISIBLE
    GlobalScope.launch {
        networkCall()
    }

}

private suspend fun networkCall() {
    withContext(Dispatchers.Default) {
        for (i in 1..50000) {
            Log.v("Network call", "$i")
        }
        runOnUiThread {
            progressBar.visibility = View.INVISIBLE
        }
    }
}

从代码中删除该行:

progressBar.visibility = View.GONE

Becuase your style is ( style="?android:attr/progressBarStyle" ), and your width/height are (wrap_content).因为你的风格是 ( style="?android:attr/progressBarStyle" ),你的宽度/高度是 (wrap_content)。 for Bar style.为酒吧风格。 you should give it value (for example 100dp to WIDTH, and 20dp to HEIGHT.你应该给它值(例如 100dp 到 WIDTH,20dp 到 HEIGHT。

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

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