Android实现加载状态视图切换效果

Android实现加载状态视图切换效果,第1张

概述关于Android加载状态视图切换,具体内容如下1.关于Android界面切换状态的介绍怎样切换界面状态?有些界面想定制自定义状态?状态如何添加点击事件?下面就为解决这些问题!

关于AndroID加载状态视图切换,具体内容如下

1.关于AndroID界面切换状态的介绍

怎样切换界面状态?有些界面想定制自定义状态?状态如何添加点击事件?下面就为解决这些问题!
内容界面
加载数据中
加载数据错误
加载后没有数据
没有网络

2.思路转变,抽取分离类管理几种状态

以前做法:

直接把这些界面include到main界面中,然后动态去切换界面,后来发现这样处理不容易复用到其他项目中,而且在activity中处理这些状态的显示和隐藏比较乱
利用子类继承父类特性,在父类中写切换状态,但有些界面如果没有继承父类,又该如何处理

现在做法:

让VIEw状态的切换和Activity彻底分离开,必须把这些状态VIEw都封装到一个管理类中,然后暴露出几个方法来实现VIEw之间的切换。
在不同的项目中可以需要的VIEw也不一样,所以考虑把管理类设计成builder模式来自由的添加需要的状态VIEw

3.关于该状态切换工具优点分析

可以自由切换内容,空数据,异常错误,加载,网络错误等5种状态
父类BaseActivity直接暴露5中状态,方便子类统一管理状态切换

/*** ================================================* 作  者:杨充* 版  本:1.0* 创建日期:2017/7/6* 描  述:抽取类* 修订历史:* ================================================*/public abstract class BaseActivity extends AppCompatActivity {  protected StatusLayoutManager statusLayoutManager;  @OverrIDe  protected voID onCreate(@Nullable Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_base_vIEw);    initStatusLayout();    initBaseVIEw();    initToolbar();    initVIEw();  }  protected abstract voID initStatusLayout();  protected abstract voID initVIEw();  /**  * 获取到布局  */  private voID initBaseVIEw() {    linearLayout ll_main = (linearLayout) findVIEwByID(R.ID.ll_main);    ll_main.addVIEw(statusLayoutManager.getRootLayout());  }  //正常展示数据状态  protected voID showContent() {    statusLayoutManager.showContent();  }  //加载数据为空时状态  protected voID showEmptyData() {    statusLayoutManager.showEmptyData();  }  //加载数据错误时状态  protected voID showError() {    statusLayoutManager.showError();  }  //网络错误时状态  protected voID showNetWorkError() {    statusLayoutManager.showNetWorkError();  }  //正在加载中状态  protected voID showLoading() {    statusLayoutManager.showLoading();  }}

当状态是加载数据失败时,点击可以刷新数据;当状态是无网络时,点击可以设置网络

/*** 点击重新刷新*/private voID initErrorDataVIEw() {  statusLayoutManager.showError();  linearLayout ll_error_data = (linearLayout) findVIEwByID(R.ID.ll_error_data);  ll_error_data.setonClickListener(new VIEw.OnClickListener() {    @OverrIDe    public voID onClick(VIEw vIEw) {      initData();      adapter.notifyDataSetChanged();      showContent();    }  });}/*** 点击设置网络*/private voID initSettingNetwork() {  statusLayoutManager.showNetWorkError();  linearLayout ll_set_network = (linearLayout) findVIEwByID(R.ID.ll_set_network);  ll_set_network.setonClickListener(new VIEw.OnClickListener() {    @OverrIDe    public voID onClick(VIEw vIEw) {      Intent intent = new Intent("androID.settings.WIRELESS_SETTINGS");      startActivity(intent);    }  });}

倘若有些页面想定制状态布局,也可以自由实现,很简单:

/*** 自定义加载数据为空时的状态布局*/private voID initEmptyDataVIEw() {  statusLayoutManager.showEmptyData();  //此处是自己定义的状态布局  **statusLayoutManager.showLayoutEmptyData(R.layout.activity_emptydata);**  linearLayout ll_empty_data = (linearLayout) findVIEwByID(R.ID.ll_empty_data);  ll_empty_data.setonClickListener(new VIEw.OnClickListener() {    @OverrIDe    public voID onClick(VIEw vIEw) {      initData();      adapter.notifyDataSetChanged();      showContent();    }  });}

4.如何实现的步骤

1).先看看状态管理器类【builder建造者模式】

loadingLayoutResID和contentLayoutResID代表等待加载和显示内容的xml文件
几种异常状态要用VIEwStub,因为在界面状态切换中loading和内容VIEw都是一直需要加载显示的,但是其他的3个只有在没数据或者网络异常的情况下才会加载显示,所以用VIEwStub来加载他们可以提高性能。

public class StateLayoutManager {  final Context context;  final VIEwStub netWorkErrorVs;  final int netWorkErrorRetryVIEwID;  final VIEwStub emptyDataVs;  final int emptyDataRetryVIEwID;  final VIEwStub errorVs;  final int errorRetryVIEwID;  final int loadingLayoutResID;  final int contentLayoutResID;  final int retryVIEwID;  final int emptyDataIconImageID;  final int emptyDataTextTipID;  final int errorIconImageID;  final int errorTextTipID;  final VLayout errorLayout;  final VLayout emptyDataLayout;  final RootFrameLayout rootFrameLayout;  final OnShowHIDeVIEwListener onShowHIDeVIEwListener;  final OnRetryListener onRetryListener;  public StateLayoutManager(Builder builder) {    this.context = builder.context;    this.loadingLayoutResID = builder.loadingLayoutResID;    this.netWorkErrorVs = builder.netWorkErrorVs;    this.netWorkErrorRetryVIEwID = builder.netWorkErrorRetryVIEwID;    this.emptyDataVs = builder.emptyDataVs;    this.emptyDataRetryVIEwID = builder.emptyDataRetryVIEwID;    this.errorVs = builder.errorVs;    this.errorRetryVIEwID = builder.errorRetryVIEwID;    this.contentLayoutResID = builder.contentLayoutResID;    this.onShowHIDeVIEwListener = builder.onShowHIDeVIEwListener;    this.retryVIEwID = builder.retryVIEwID;    this.onRetryListener = builder.onRetryListener;    this.emptyDataIconImageID = builder.emptyDataIconImageID;    this.emptyDataTextTipID = builder.emptyDataTextTipID;    this.errorIconImageID = builder.errorIconImageID;    this.errorTextTipID = builder.errorTextTipID;    this.errorLayout = builder.errorLayout;    this.emptyDataLayout = builder.emptyDataLayout;    rootFrameLayout = new RootFrameLayout(this.context);    VIEwGroup.LayoutParams layoutParams = new VIEwGroup.LayoutParams(VIEwGroup.LayoutParams.MATCH_PARENT,VIEwGroup.LayoutParams.MATCH_PARENT);    rootFrameLayout.setLayoutParams(layoutParams);    rootFrameLayout.setStatusLayoutManager(this);  }  /**  * 显示loading  */  public voID showLoading() {    rootFrameLayout.showLoading();  }  /**  * 显示内容  */  public voID showContent() {    rootFrameLayout.showContent();  }  /**  * 显示空数据  */  public voID showEmptyData(int iconImage,String textTip) {    rootFrameLayout.showEmptyData(iconImage,textTip);  }  /**  * 显示空数据  */  public voID showEmptyData() {    showEmptyData(0,"");  }  /**  * 显示空数据  */  public voID showLayoutEmptyData(Object... objects) {    rootFrameLayout.showLayoutEmptyData(objects);  }  /**  * 显示网络异常  */  public voID showNetWorkError() {    rootFrameLayout.showNetWorkError();  }  /**  * 显示异常  */  public voID showError(int iconImage,String textTip) {    rootFrameLayout.showError(iconImage,textTip);  }  /**  * 显示异常  */  public voID showError() {    showError(0,"");  }  public voID showLayoutError(Object... objects) {    rootFrameLayout.showLayoutError(objects);  }  /**  * 得到root 布局  */  public VIEw getRootLayout() {    return rootFrameLayout;  }  public static final class Builder {    private Context context;    private int loadingLayoutResID;    private int contentLayoutResID;    private VIEwStub netWorkErrorVs;    private int netWorkErrorRetryVIEwID;    private VIEwStub emptyDataVs;    private int emptyDataRetryVIEwID;    private VIEwStub errorVs;    private int errorRetryVIEwID;    private int retryVIEwID;    private int emptyDataIconImageID;    private int emptyDataTextTipID;    private int errorIconImageID;    private int errorTextTipID;    private VLayout errorLayout;    private VLayout emptyDataLayout;    private OnShowHIDeVIEwListener onShowHIDeVIEwListener;    private OnRetryListener onRetryListener;    public Builder(Context context) {      this.context = context;    }    /**    * 自定义加载布局    */    public Builder loadingVIEw(@LayoutRes int loadingLayoutResID) {      this.loadingLayoutResID = loadingLayoutResID;      return this;    }    /**    * 自定义网络错误布局    */    public Builder netWorkErrorVIEw(@LayoutRes int newWorkErrorID) {      netWorkErrorVs = new VIEwStub(context);      netWorkErrorVs.setLayoutResource(newWorkErrorID);      return this;    }    /**    * 自定义加载空数据布局    */    public Builder emptyDataVIEw(@LayoutRes int noDataVIEwID) {      emptyDataVs = new VIEwStub(context);      emptyDataVs.setLayoutResource(noDataVIEwID);      return this;    }    /**    * 自定义加载错误布局    */    public Builder errorVIEw(@LayoutRes int errorVIEwID) {      errorVs = new VIEwStub(context);      errorVs.setLayoutResource(errorVIEwID);      return this;    }    /**    * 自定义加载内容正常布局    */    public Builder contentVIEw(@LayoutRes int contentLayoutResID) {      this.contentLayoutResID = contentLayoutResID;      return this;    }    public Builder errorLayout(VLayout errorLayout) {      this.errorLayout = errorLayout;      this.errorVs = errorLayout.getLayoutVs();      return this;    }    public Builder emptyDataLayout(VLayout emptyDataLayout) {      this.emptyDataLayout = emptyDataLayout;      this.emptyDataVs = emptyDataLayout.getLayoutVs();      return this;    }    public Builder netWorkErrorRetryVIEwID(int netWorkErrorRetryVIEwID) {      this.netWorkErrorRetryVIEwID = netWorkErrorRetryVIEwID;      return this;    }    public Builder emptyDataRetryVIEwID(int emptyDataRetryVIEwID) {      this.emptyDataRetryVIEwID = emptyDataRetryVIEwID;      return this;    }    public Builder errorRetryVIEwID(int errorRetryVIEwID) {      this.errorRetryVIEwID = errorRetryVIEwID;      return this;    }    public Builder retryVIEwID(int retryVIEwID) {      this.retryVIEwID = retryVIEwID;      return this;    }    public Builder emptyDataIconImageID(int emptyDataIconImageID) {      this.emptyDataIconImageID = emptyDataIconImageID;      return this;    }    public Builder emptyDataTextTipID(int emptyDataTextTipID) {      this.emptyDataTextTipID = emptyDataTextTipID;      return this;    }    public Builder errorIconImageID(int errorIconImageID) {      this.errorIconImageID = errorIconImageID;      return this;    }    public Builder errorTextTipID(int errorTextTipID) {      this.errorTextTipID = errorTextTipID;      return this;    }    public Builder onShowHIDeVIEwListener(OnShowHIDeVIEwListener onShowHIDeVIEwListener) {      this.onShowHIDeVIEwListener = onShowHIDeVIEwListener;      return this;    }    public Builder onRetryListener(OnRetryListener onRetryListener) {      this.onRetryListener = onRetryListener;      return this;    }    public StateLayoutManager build() {      return new StateLayoutManager(this);    }  }  public static Builder newBuilder(Context context) {    return new Builder(context);  }}

2).大约5种状态,如何管理这些状态?添加到集合中,AndroID中选用SparseArray比HashMap更省内存,在某些条件下性能更好,主要是因为它避免了对key的自动装箱(int转为Integer类型),它内部则是通过两个数组来进行数据存储的,一个存储key,另外一个存储value,为了优化性能,它内部对数据还采取了压缩的方式来表示稀疏数组的数据,从而节约内存空间

/**存放布局集合 */private SparseArray<VIEw> layoutSparseArray = new SparseArray();
/**将布局添加到集合 */……private voID addLayoutResID(@LayoutRes int layoutResID,int ID) {  VIEw resVIEw = LayoutInflater.from(mStatusLayoutManager.context).inflate(layoutResID,null);  **layoutSparseArray.put(ID,resVIEw);**  addVIEw(resVIEw);}

3).当显示某个布局时,调用的方法如下

方法里面通过ID判断来执行不同的代码,首先判断VIEwStub是否为空,如果为空就代表没有添加这个VIEw就返回false,不为空就加载VIEw并且添加到集合当中,然后调用showHIDeVIEwByID方法显示隐藏VIEw,retryLoad方法是给重试按钮添加事件

/*** 显示loading*/public voID showLoading() {  if (layoutSparseArray.get(LAYOUT_LOADING_ID) != null)    **showHIDeVIEwByID**(LAYOUT_LOADING_ID);}/*** 显示内容*/public voID showContent() {  if (layoutSparseArray.get(LAYOUT_CONTENT_ID) != null)    **showHIDeVIEwByID**(LAYOUT_CONTENT_ID);}/*** 显示空数据*/public voID showEmptyData(int iconImage,String textTip) {  if (**inflateLayout**(LAYOUT_EMPTYDATA_ID)) {    showHIDeVIEwByID(LAYOUT_EMPTYDATA_ID);    emptyDataVIEwAddData(iconImage,textTip);  }}/*** 显示网络异常*/public voID showNetWorkError() {  if (**inflateLayout**(LAYOUT_NETWORK_ERROR_ID))    showHIDeVIEwByID(LAYOUT_NETWORK_ERROR_ID);}/*** 显示异常*/public voID showError(int iconImage,String textTip) {  if (**inflateLayout**(LAYOUT_ERROR_ID)) {    showHIDeVIEwByID(LAYOUT_ERROR_ID);    errorVIEwAddData(iconImage,textTip);  }}//调用inflateLayout方法,方法返回true然后调用showHIDeVIEwByID方法private boolean inflateLayout(int ID) {  boolean isShow = true;  if (layoutSparseArray.get(ID) != null) return isShow;  switch (ID) {    case LAYOUT_NETWORK_ERROR_ID:      if (mStatusLayoutManager.netWorkErrorVs != null) {        VIEw vIEw = mStatusLayoutManager.netWorkErrorVs.inflate();        retryLoad(vIEw,mStatusLayoutManager.netWorkErrorRetryVIEwID);        layoutSparseArray.put(ID,vIEw);        isShow = true;      } else {        isShow = false;      }      break;    case LAYOUT_ERROR_ID:      if (mStatusLayoutManager.errorVs != null) {        VIEw vIEw = mStatusLayoutManager.errorVs.inflate();        if (mStatusLayoutManager.errorLayout != null) mStatusLayoutManager.errorLayout.setVIEw(vIEw);        retryLoad(vIEw,mStatusLayoutManager.errorRetryVIEwID);        layoutSparseArray.put(ID,vIEw);        isShow = true;      } else {        isShow = false;      }      break;    case LAYOUT_EMPTYDATA_ID:      if (mStatusLayoutManager.emptyDataVs != null) {        VIEw vIEw = mStatusLayoutManager.emptyDataVs.inflate();        if (mStatusLayoutManager.emptyDataLayout != null) mStatusLayoutManager.emptyDataLayout.setVIEw(vIEw);        retryLoad(vIEw,mStatusLayoutManager.emptyDataRetryVIEwID);        layoutSparseArray.put(ID,vIEw);        isShow = true;      } else {        isShow = false;      }      break;  }  return isShow;}

4).然后在根据ID隐藏布局

通过ID找到需要显示的VIEw并且显示它,隐藏其他VIEw,如果显示隐藏监听事件不为空,就分别调用它的显示和隐藏方法

/*** 根据ID显示隐藏布局* @param ID*/private voID showHIDeVIEwByID(int ID) {  for (int i = 0; i < layoutSparseArray.size(); i++) {    int key = layoutSparseArray.keyAt(i);    VIEw valueVIEw = layoutSparseArray.valueAt(i);    //显示该vIEw    if(key == ID) {      valueVIEw.setVisibility(VIEw.VISIBLE);      if(mStatusLayoutManager.onShowHIDeVIEwListener != null) mStatusLayoutManager.onShowHIDeVIEwListener.onShowVIEw(valueVIEw,key);    } else {      if(valueVIEw.getVisibility() != VIEw.GONE) {        valueVIEw.setVisibility(VIEw.GONE);        if(mStatusLayoutManager.onShowHIDeVIEwListener != null) mStatusLayoutManager.onShowHIDeVIEwListener.onHIDeVIEw(valueVIEw,key);      }    }  }}

5).最后看看重新加载方法

/*** 重试加载*/private voID retryLoad(VIEw vIEw,int ID) {  VIEw retryVIEw = vIEw.findVIEwByID(mStatusLayoutManager.retryVIEwID != 0 ? mStatusLayoutManager.retryVIEwID : ID);  if (retryVIEw == null || mStatusLayoutManager.onRetryListener == null) return;  retryVIEw.setonClickListener(new OnClickListener() {    @OverrIDe    public voID onClick(VIEw v) {      mStatusLayoutManager.onRetryListener.onRetry();    }  });}

5.使用方法介绍

1).直接在Activity中添加代码

@OverrIDeprotected voID initStatusLayout() {  statusLayoutManager = StateLayoutManager.newBuilder(this)      .contentVIEw(R.layout.activity_content_data)      .emptyDataVIEw(R.layout.activity_empty_data)      .errorVIEw(R.layout.activity_error_data)      .loadingVIEw(R.layout.activity_loading_data)      .netWorkErrorVIEw(R.layout.activity_networkerror)      .onRetryListener(new OnRetryListener() {        @OverrIDe        public voID onRetry() {          //为重试加载按钮的监听事件        }      })      .onShowHIDeVIEwListener(new OnShowHIDeVIEwListener() {        @OverrIDe        public voID onShowVIEw(VIEw vIEw,int ID) {          //为状态VIEw显示监听事件        }        @OverrIDe        public voID onHIDeVIEw(VIEw vIEw,int ID) {          //为状态VIEw隐藏监听事件        }      })      .build();}

2).在父类中重写以下几个方法,子类直接继承就行

//正常展示数据状态protected voID showContent() {  statusLayoutManager.showContent();}//加载数据为空时状态protected voID showEmptyData() {  statusLayoutManager.showEmptyData();}//加载数据错误时状态protected voID showError() {  statusLayoutManager.showError();}//网络错误时状态protected voID showNetWorkError() {  statusLayoutManager.showNetWorkError();}//正在加载中状态protected voID showLoading() {  statusLayoutManager.showLoading();}

3).更加详细的介绍,可以直接参考Demo
https://github.com/yangchong211/YCStateLayout

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android实现加载状态视图切换效果全部内容,希望文章能够帮你解决Android实现加载状态视图切换效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1144966.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存