在Android应用中使用AsyncTask在for循环中更新ProgressBar

问题描述 投票:0回答:1

我正在尝试创建一个应用程序,该应用程序要使用AsyncTask在for循环中更新ProgressBar。最好的方法是向您显示我要执行的操作的伪代码

button.setOnClickListener{
    for(int i=0; i<5000; i++)
    {
        doSomeHeavyStuff();
        UpdateProgressBarAsyncTask(i).execute()
    }
}

这是我到目前为止所拥有的

MainActivity:

    class MainActivity : AppCompatActivity() {

    var progress:ProgressBar? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        progress = findViewById(R.id.progress)
        val buttonStart = findViewById<TextView>(R.id.button)
        var maxNumber = 5000
        buttonStart.setOnClickListener{
            for(i in 0 until maxNumber)
            {
                HeavyStuff()
                ProgressTask(i,progress!!,maxNumber,this).execute()
            }
        }


    }

internal class ProgressTask (var actual:Int, var progress: ProgressBar, var max: Int, var context: Activity): AsyncTask <Void, Int, Int>()
    {
        override fun onPreExecute() {
            super.onPreExecute()
            progress.visibility = View.VISIBLE
            progress.max = max
        }

        override fun onProgressUpdate(vararg values: Int?) {
            super.onProgressUpdate(*values)
            progress.setProgress(values[0]!!)
        }

        override fun doInBackground(vararg params: Void?): Int? {
            publishProgress(actual)
            return null
        }

        override fun onPostExecute(result: Int?) {
            super.onPostExecute(result)
            progress.visibility = View.INVISIBLE
            Toast.makeText(context, "Finished!", Toast.LENGTH_SHORT).show()
        }
    }

XML:

<ProgressBar
        android:id="@+id/progress"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginBottom="24dp"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

我想念这里的东西吗?现在,它在HeavyStuff()完成后显示ProgressBar,因此在for循环中未显示。我在这里想念什么?

你们能帮我吗?

谢谢

android for-loop kotlin android-asynctask progress-bar
1个回答
0
投票

实际上,我认为在doBackground函数内部都需要包含大量内容和For循环(在主线程中调用大量内容将冻结应用程序UI并导致ANR,请参见下面的代码:] >

class MainActivity : AppCompatActivity() {

    var progress: ProgressBar? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        progress = findViewById(R.id.progress)
        buttonStart.setOnClickListener {
            ProgressTask(progress!!, 5000, this).execute()
        }
    }

    internal class ProgressTask(
        var progress: ProgressBar,
        var max: Int,
        var context: Activity
    ) : AsyncTask<Void, Int, Int>() {
        override fun onPreExecute() {
            super.onPreExecute()
            progress.visibility = View.VISIBLE
            progress.max = max
        }

        override fun onProgressUpdate(vararg values: Int?) {
            super.onProgressUpdate(*values)
            progress.setProgress(values[0]!!)
        }

        override fun doInBackground(vararg params: Void?): Int? {
            for (i in 0 until 5000) {
                HeavyStuff()
                publishProgress(i)
            }
            return null
        }

        override fun onPostExecute(result: Int?) {
            super.onPostExecute(result)
            progress.visibility = View.INVISIBLE
            Toast.makeText(context, "Finished!", Toast.LENGTH_SHORT).show()
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.