一、引入@IntDef的jar包:
compile 'com.android.support:support-annotations:25.3.1'
二、使用:
1.使用@IntDef定义状态
/**
* 定义状态
*/
public class NodataState {
@IntDef({State.NO_NETWORK, State.CONNECT_ERROR, State.NO_DATA})
@Retention(RetentionPolicy.SOURCE)
public @interface State {
int NO_NETWORK = 1; //当前网络不可用
int CONNECT_ERROR = 2; //数据加载失败
int NO_DATA = 3; //暂无数据
}
}
2.使用定义的状态值:
public void onRefreshClick() {
switch (mType) {
case NodataState.State.NO_NETWORK:
break;
case NodataState.State.NO_DATA:
break;
case NodataState.State.CONNECT_ERROR:
break;
}
}