Android實現加載等待展示
本文實例為大家分享了Android實現加載等待展示的具體代碼,供大家參考,具體內容如下
package com.zhcs.gis.app.modulecore.core.component.tool;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import com.zhcs.gis.app.modulecore.R;
import java.lang.ref.WeakReference;
import androidx.appcompat.app.AlertDialog;
public class LoadingDialogUtils_M {
private static AlertDialog loadingDialog;
private static WeakReference<Activity> reference;
private static void init(Activity act) {
init(act, -1);
}
private static void init(Activity activity, int res) {
if (loadingDialog == null || reference == null || reference.get() == null || reference.get().isFinishing()) {
reference = new WeakReference<>(activity);
loadingDialog = new AlertDialog.Builder(reference.get()).create();
if (res > 0) {
View view = LayoutInflater.from(activity).inflate(res, null);
loadingDialog.setView(view);
loadingDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
// loadingDialog.setContentView(res);
} else {
loadingDialog.setMessage("加載中...");
}
loadingDialog.setCancelable(false);
}
}
public static void setCancelable(boolean b) {
if (loadingDialog == null) return;
loadingDialog.setCancelable(b);
}
/**
* 顯示等待框
*/
public static void show(Activity act) {
show(act, false);
}
public static void show(Activity act, boolean isCancelable) {
show(act, R.layout.dialog_loading, isCancelable);
}
public static void show(Activity activity, int res, boolean isCancelable) {
init(activity, res);
loadingDialog.show();
setCancelable(isCancelable);
}
/**
* 隱藏等待框
*/
public static void dismiss() {
if (loadingDialog != null && loadingDialog.isShowing()) {
loadingDialog.dismiss();
loadingDialog = null;
reference = null;
}
}
}
在使用的時候,在baseactivity里面這樣設置:
public void showLoading(boolean show) {
if (show) {
LoadingDialogUtils_M.show(context, true);
} else {
LoadingDialogUtils_M.dismiss();
}
}
附件:
這是dialog_loading布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent">
<ProgressBar
android:id="@+id/progress"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:indeterminateDrawable="@drawable/bg_progressbar" />
</RelativeLayout>
這是bg_progressbar配置:
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@mipmap/img_loading" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="1080" />
最后就是加載的圖片img_loading的樣式,可以自定義,隨便上網找一個就可以,看著好看就行。

最后就可以使用了,挺有用的一個小技巧,加載用的,也可以用其他的方式實現,只要效果一樣就行。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android Studio創(chuàng)建AIDL文件并實現進程間通訊實例
本篇文章主要介紹了Android Studio創(chuàng)建AIDL文件并實現進程間通訊實例,具有一定的參考價值,有興趣可以了解一下。2017-04-04
Android中CountDownTimer倒計時器用法實例
這篇文章主要介紹了Android中CountDownTimer倒計時器用法,以實例形式分析了Android中CountDownTimer類的相關使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
Android 使用Vitamio打造自己的萬能播放器(9)—— 在線播放 (在線電視)
本文主要介紹Android 使用Vitamio開發(fā)播放器,實現在線電視播放,這里提供效果圖和實例代碼以便大家參考,2016-07-07
Android獲取LinearLayout的寬度和高度示例代碼
這篇文章主要介紹了android獲取LinearLayout的寬度和高度,如果想直接獲取在布局文件中定義的組件的寬度和高度,可以直接使用View.getLayoutParams().width和View.getLayoutParams().height,本文結合示例代碼介紹的非常詳細,需要的朋友可以參考下2023-08-08
利用Warensoft Stock Service編寫高頻交易軟件
本文主要介紹了利用Warensoft Stock Service編寫高頻交易軟件的方法步驟,具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01

