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()
}
|