kotlin 使用Dialog構(gòu)建一個(gè)通用的進(jìn)度條提示的后臺請求

最終使用代碼如下

GlobalScope.progress(context,{
         //這里寫后臺請求代碼比如
        val users = RetrofitServiceManager.create(PosApi::class.java).searchUser(user).get()
    }){
       //請求完后的操作
     adapter.notifyDataSetChanged()
 }

第一步構(gòu)建一個(gè)進(jìn)度條提示的Dialog

1.布局文件如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@drawable/group_box_progress"
    android:padding="@dimen/padding_m">

    <androidx.core.widget.ContentLoadingProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/loadingImageView"
        style="?android:attr/progressBarStyleSmall"/>

    <TextView
        android:id="@+id/id_tv_loadingmsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="@dimen/padding_s"
        android:layout_marginRight="@dimen/padding_m"
        android:textColor="@color/colorContent"
        android:textSize="15dp" />

</LinearLayout>

2.代碼如下

class CustomProgressDialog(context: Context,theme: Int,strMessage: String) : Dialog(context, theme) {
    constructor(context: Context, strMessage: String) : this(context,
        R.style.CustomProgressDialog,strMessage)

    fun setMessage(message: String) {
        id_tv_loadingmsg.text = message
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        if (!hasFocus) {
            dismiss()
        }
    }
    init {
        this.setContentView(R.layout.customprogressdialog)
        this.window?.attributes?.gravity = Gravity.CENTER
        this.window?.attributes?.dimAmount = 0.1f
        id_tv_loadingmsg.text = strMessage
        setCanceledOnTouchOutside(false)
        loadingImageView.indeterminateDrawable.setColorFilter(ContextCompat.getColor(context, R.color.colorBlue),PorterDuff.Mode.MULTIPLY )
    }
}

3.樣式也給上

<style name="CustomProgressDialog" parent="@style/CustomDialog">
        <item name="android:windowBackground">@drawable/group_box_progress</item>
        <item name="android:windowNoTitle">true</item>
</style>
//group_box_progress文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#5A5959" />
    <corners android:radius="5dp"/>
    <stroke android:color="@color/colorContentDark" android:width="0.1dp" />
</shape>

第二步構(gòu)建協(xié)程

fun GlobalScope.progress(context: Context, back: (() -> Unit)?, main: (() -> Unit)?){
    val progressDialog = CustomProgressDialog(context,"正常請求數(shù)據(jù),請稍后......")
    progressDialog.show()
    launch(Dispatchers.IO){
        try {
            back?.invoke()
        }catch (e: Exception){
            e.printStackTrace()
        }
        withContext(Dispatchers.Main){
            progressDialog.dismiss()
            main?.invoke()
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容