温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android实现加载等待展示

发布时间:2020-10-26 04:05:16 来源:脚本之家 阅读:144 作者:马占柱 栏目:开发技术

本文实例为大家分享了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实现加载等待展示

最后就可以使用了,挺有用的一个小技巧,加载用的,也可以用其他的方式实现,只要效果一样就行。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI