当任务在 Kotlin 中 运行 时如何绘制布局

How can I draw a layout when a task is running in Kotlin

我解释一下情况,我做了一个算法显示通过所有点的最短路径,这个算法需要一点时间运行所以我想设置一个进度条来诱导用户应用程序的部分尚未冻结但正在执行计算,为此我只是创建了一个带有进度条的布局但是当我执行代码时没有显示任何内容(布局)但显示了我的算法的结果,是否有命令显示它?

progress_bar.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:ignore="ExtraText"
    android:id="@+id/download_root"
    android:background="@color/background_main"
   tools:context=".ui.farm.DaturaFragment">


   <com.mikhaellopez.circularprogressbar.CircularProgressBar
       android:id="@+id/route_progress"
       android:layout_width="@dimen/sync_central_icon_height"
       android:layout_height="@dimen/sync_central_icon_height"
       app:cpb_background_progressbar_color="@color/blue_dark"
       app:cpb_background_progressbar_width="14dp"
       app:cpb_indeterminate_mode="true"
       app:cpb_progress="0"
       app:cpb_progress_direction="to_right"
       app:cpb_progress_max="100"
       app:cpb_progressbar_color="@color/green"
       app:cpb_progressbar_width="7dp"
       app:cpb_round_border="true"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toTopOf="parent"
       app:layout_constraintVertical_bias="0.405"
       app:layout_constraintVertical_chainStyle="packed" />


   <TextView
       android:id="@+id/download_subtitle"
       android:layout_width="500dp"
       android:layout_height="wrap_content"
       android:background="@color/background_main"
       android:text="Calculation of the current purification route?"
       android:textAlignment="center"
       android:textColor="@color/white"
       android:textFontWeight="600"
       android:textSize="@dimen/text_large"
       android:textStyle="bold"
       android:visibility="gone"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintHorizontal_bias="0.505"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toTopOf="parent"
       app:layout_constraintVertical_bias="0.57"
       tools:visibility="visible" />


</androidx.constraintlayout.widget.ConstraintLayout>

我的 mainActivity.xml:

<include
            android:id="@+id/dialogue_route"
            layout="@layout/route_datura_dialog"
            android:layout_width="match_parent"
            android:layout_height="300dp"

            android:layout_marginTop="200dp"
            android:visibility="invisible" />
 datura_path_epuration.setOnClickListener {
            if (epurationStateButton == 0) {
                if (pointsOnThefield != null && !mapViewHelper.getEpurationFinished(pointsOnThefield!!)) {
                    if (buttonState == 0) {
                        Toast.makeText(context, "Please enable geolocation", Toast.LENGTH_SHORT).show()
                    } else {

                        displayDialogueRoute()

                        mapViewHelper.displayEpurationPath(pointsOnThefield!!)

                        removeDialogueRoute()

                    }
                }
                else
                    Toast.makeText(context, "All daturas have been purified", Toast.LENGTH_SHORT).show()
                epurationStateButton = 1
            }else {
                mapViewHelper.deletePolyLine()
                epurationStateButton = 0
            }
        }
private fun displayDialogueRoute(){
        dialogue_route.visibility = View.VISIBLE
        datura_location_cta.visibility = View.INVISIBLE
        datura_path_epuration.visibility = View.INVISIBLE
        detail_close.visibility = View.INVISIBLE
        datura_non_eputration_cta.visibility = View.INVISIBLE
        datura_eputration_cta.visibility= View.INVISIBLE
    }
    private fun removeDialogueRoute(){
        dialogue_route.visibility = View.INVISIBLE
        datura_location_cta.visibility = View.VISIBLE
        datura_path_epuration.visibility = View.VISIBLE
        detail_close.visibility = View.VISIBLE
    }

似乎存在线程问题。 long-运行 任务可能会在计算期间阻塞 UI。

在后台尝试穷举算法并在后台计算完成后更新 UI(在本例中为进度条)怎么样?

您可以使用流行的 Kotlin-Coroutine 来实现这一点。

你可以复制粘贴试试:

GlobalScope.launch(Dispatchers.Main) { 
           //Main UI Thread
           withContext(Dispatchers.IO) {
           //Background Thread
           //Perform long-running task here
           }
           //Task finished, Update Progress Bar here
        }