栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android实现加载等待展示

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 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布局文件:



 
  

这是bg_progressbar配置:


最后就是加载的图片img_loading的样式,可以自定义,随便上网找一个就可以,看着好看就行。

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/153871.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号