Kotlin MeterialDialog实现loading

1.导入

implementation “com.afollestad.material-dialogs:core:3.3.0”
implementation “com.afollestad.material-dialogs:lifecycle:3.3.0”

2.布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@color/white"
    app:cardCornerRadius="12dp">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:indeterminateTint="@color/colorProgress"
            android:indeterminateTintMode="src_atop" />

        <TextView
            android:id="@+id/loading_tips"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="加载中..."
            android:textColor="#666"
            android:textSize="16dp" />
    </LinearLayout>
</androidx.cardview.widget.CardView>

3.完整方法

//loading框
@SuppressLint("StaticFieldLeak")
private var loadingDialog: MaterialDialog? = null
private var showTime : Long = 0;
private const val showLoadingTime = 1000;
/**
 * 打开等待框
 */
fun AppCompatActivity.showLoadingExt(message: String = "请求网络中") {
    if (!this.isFinishing) {
        if (loadingDialog == null) {
            loadingDialog = MaterialDialog(this)
                    .cancelable(true)
                    .cancelOnTouchOutside(false)
                    .cornerRadius(12f)
                    .customView(R.layout.layout_custom_progress_dialog_view)
                    .lifecycleOwner(this)
            loadingDialog?.getCustomView()?.run {
                this.findViewById<TextView>(R.id.loading_tips).text = message
//                this.findViewById<ProgressBar>(R.id.progressBar).indeterminateTintList = SettingUtil.getOneColorStateList(this@showLoadingExt)
            }
        }
        showTime = System.currentTimeMillis();
        loadingDialog?.show()
    }
}

/**
 * 打开等待框
 */
fun Fragment.showLoadingExt(message: String = "请求网络中") {
    activity?.let {
        if (!it.isFinishing) {
            if (loadingDialog == null) {
                loadingDialog = MaterialDialog(it)
                    .cancelable(true)
                    .cancelOnTouchOutside(false)
                    .cornerRadius(12f)
                    .customView(R.layout.layout_custom_progress_dialog_view)
                    .lifecycleOwner(this)
                loadingDialog?.getCustomView()?.run {
                    this.findViewById<TextView>(R.id.loading_tips).text = message
//                    this.findViewById<ProgressBar>(R.id.progressBar).indeterminateTintList = SettingUtil.getOneColorStateList(it)
                }
            }
            showTime = System.currentTimeMillis();
            loadingDialog?.show()
        }
    }
}

/**
 * 关闭等待框
 */
fun Activity.dismissLoadingExt() {
    if (System.currentTimeMillis() - showTime < showLoadingTime){
        Timer().schedule(showLoadingTime-(System.currentTimeMillis() - showTime )){
            loadingDialog?.dismiss()
            loadingDialog = null        }
    }else{
        loadingDialog?.dismiss()
        loadingDialog = null
    }
}

/**
 * 关闭等待框
 */
fun Fragment.dismissLoadingExt() {
    if (System.currentTimeMillis() - showTime < showLoadingTime){
        Timer().schedule(showLoadingTime-(System.currentTimeMillis() - showTime )){
            loadingDialog?.dismiss()
            loadingDialog = null        }
    }else{
        loadingDialog?.dismiss()
        loadingDialog = null
    }
}

4.调用方法

展示弹窗

showLoadingExt("加载中...")

隐藏弹窗保证在UI线程调用

dismissLoadingExt()

如果不在UI线程

 runOnUiThread {
            dismissLoadingExt()
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值