Android實(shí)現(xiàn)加載等待展示
本文實(shí)例為大家分享了Android實(shí)現(xiàn)加載等待展示的具體代碼,供大家參考,具體內(nèi)容如下
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;
}
}
}
在使用的時(shí)候,在baseactivity里面這樣設(shè)置:
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的樣式,可以自定義,隨便上網(wǎng)找一個(gè)就可以,看著好看就行。

最后就可以使用了,挺有用的一個(gè)小技巧,加載用的,也可以用其他的方式實(shí)現(xiàn),只要效果一樣就行。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android入門之ActivityGroup+GridView實(shí)現(xiàn)Tab分頁標(biāo)簽的方法
這篇文章主要介紹了Android入門之ActivityGroup+GridView實(shí)現(xiàn)Tab分頁標(biāo)簽的方法,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08
Android Dialog仿ios9中UIAlertController控件
這篇文章主要為大家詳細(xì)介紹了Android Dialog仿ios9中UIAlertController控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
android實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android中使用socket使底層和framework通信的實(shí)現(xiàn)方法
native和framework的通信是通過jni,但是這一般只是framework調(diào)用native,native如果有消息要怎樣通知上層 呢?android中GSP模塊提供一種解決思路,但是實(shí)現(xiàn)有些復(fù)雜,這里介紹一種使用socket通信的方法可以使native和framework自由通信,感興趣的朋友一起看看吧2016-11-11
Android Studio 3.6 layout文件text模式切換問題
這篇文章主要介紹了Android Studio 3.6 layout文件text模式切換問,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Android實(shí)現(xiàn)樹形層級(jí)ListView
這篇文章主要介紹了Android實(shí)現(xiàn)樹形層級(jí)ListView的相關(guān)資料,需要的朋友可以參考下2016-02-02

