/**
* ================================================
* 作 者:杨充
* 版 本: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();
}
});
}
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);
}
}
/**存放布局集合 */ 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);
}
/**
* 显示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;
}
/**
* 根据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);
}
}
}
}
/**
* 重试加载
*/
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();
}
});
}
@Override
protected 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();
}
//正常展示数据状态
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();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有