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

android使用DataBinding来设置空状态

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

android使用DataBinding来设置空状态

写在前面

在平时的开发之中,我们需要对于数据加载的情况进行展示:

  1. 空数据
  2. 网络异常
  3. 加载中等等情况

现在设置页面状态的方式有多种,由于笔者近期一直在使用databinding,而数据绑定通过改变模型来展示view的方式和状态页的设置也满契合的。

所以这里就讲讲使用databinding来设置android中的各种状态页。很简单,先看看效果


首先

在app的build.gradle文件中开启databinding

android{
  ...
  dataBinding {
    enabled = true
  }
}

我们先定义一些用于状态的注解EmptyState


@IntDef({NORMAL, PROGRESS, EMPTY, NET_ERROR, NOT_AVAILABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface EmptyState {

  int NORMAL = -1; //正常
  int PROGRESS = -2;//显示进度条

  int EMPTY = 11111; //列表数据为空
  int NET_ERROR = 22222; //网络未连接
  int NOT_AVAILABLE = 33333; //服务器不可用

  //...各种页面的空状态,可以自己定义、添加

}

再自定义一个异常EmptyException用于显示我们需要的状态信息


public class EmptyException extends Exception {

  private int code;

  public EmptyException(@EmptyState int code) {
    super();
    this.code = code;
  }


  @EmptyState
  public int getCode() {
    return code;
  }

  public void setCode(@EmptyState int code) {
    this.code = code;
  }
}

现在,大多数展示状态页的控件都会提供

  1. 加载中的进度条
  2. 错误信息
  3. 空状态
  4. ...

所以我们的目标也是显示这些

布局

以数据绑定的形式进行布局,使用StateModel来控制状态页展示的消息




  

    

    
  

  

    

    

      


      


    
  


布局文件中有几个方法

  1. empty 用于控制状态页是显示还是隐藏,数据加载正常(即状态为NORMAL)的时候隐藏,否则展示
  2. isProgress 是否显示加载中,如果显示进度条(即状态为PROGRESS),就隐藏异常页
  3. emptyIconRes 显示状态的图片信息
  4. currentStateLabel 显示状态的文字消息

我们定义状态的ViewModel ,就叫StateModel,来控制状态



public class StateModel extends baseObservable {

  private Context mContext = MyApp.instance();

  @EmptyState
  private int emptyState = EmptyState.NORMAL;

  private boolean empty;

  public int getEmptyState() {
    return emptyState;
  }

  
  public void setEmptyState(@EmptyState int emptyState) {
    this.emptyState = emptyState;
    notifyChange();
  }

  
  public boolean isProgress() {
    return this.emptyState == EmptyState.PROGRESS;
  }

  
  public void bindThrowable(Throwable e) {
    if (e instanceof EmptyException) {
      @EmptyState
      int code = ((EmptyException) e).getCode();

      setEmptyState(code);
    }
  }

  public boolean isEmpty() {
    return this.emptyState != EmptyState.NORMAL;
  }

  
  @Bindable
  public String getCurrentStateLabel() {

    switch (emptyState) {
      case EmptyState.EMPTY:
 return mContext.getString(R.string.no_data);
      case EmptyState.NET_ERROR:
 return mContext.getString(R.string.please_check_net_state);
      case EmptyState.NOT_AVAILABLE:
 return mContext.getString(R.string.server_not_avaliabe);
      default:
 return mContext.getString(R.string.no_data);
    }
  }

  
  @Bindable
  public Drawable getEmptyIconRes() {
    switch (emptyState) {
      case EmptyState.EMPTY:
 return ContextCompat.getDrawable(mContext, R.drawable.ic_visibility_off_green_400_48dp);
      case EmptyState.NET_ERROR:
 return ContextCompat.getDrawable(mContext, R.drawable.ic_signal_wifi_off_green_400_48dp);
      case EmptyState.NOT_AVAILABLE:
 return ContextCompat.getDrawable(mContext, R.drawable.ic_cloud_off_green_400_48dp);
      default:
 return ContextCompat.getDrawable(mContext, R.drawable.ic_visibility_off_green_400_48dp);
    }
  }

}

很普通的视图模型,主要有几个用于判断状态显示的方法

  1. bindThrowable 根据异常显示状态
  2. setEmptyState 方法用来设置当前的状态,通过notifyChange来通知布局文件改变

下面讲讲实际运用:

在activity或者fragment布局中,添加状态页的布局




  

    

    

  

  

    


      

 


      


      

    
  


最后在activity或者fragment中我们只需要通过state.bindThrowable()和state.setEmptyState()方法便可以轻松设置各种各样的状态。

loadData().subscribe(new Subscriber>() {

  @Override
  public void onStart() {
    super.onStart();
    if (!mMainBinding.refreshLayout.isRefreshing()) {
      mStateModel.setEmptyState(EmptyState.PROGRESS);
    }
  }

  @Override
  public void onCompleted() {
    mStateModel.setEmptyState(EmptyState.NORMAL);

  }

  @Override
  public void onError(Throwable e) {
    mMainBinding.refreshLayout.setRefreshing(false);
    mStateModel.bindThrowable(e);
    Toast.makeText(MainActivity.this, mStateModel.getCurrentStateLabel(), Toast.LENGTH_SHORT).show();


  }

  @Override
  public void onNext(List contributors) {
    mMainBinding.refreshLayout.setRefreshing(false);
    if (contributors == null || contributors.isEmpty()) {
      onError(new EmptyException(EmptyState.EMPTY));
    } else {
      mMainBinding.contentTv.setText(contributors.toString());
    }
  }
});

写在最后

对于要使用数据来控制视图状态的,使用databinding实在是一个事半功倍的方式。而且也十分容易理解。

最后demo地址:StateBinding_jb51.rar

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

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

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

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