四种状态:正在加载中,加载成功,网络错误,内容为空
所以写一个可以复用的类很重要,避免重复写代码
BaseApplication中
private static Handler sHandler = null;
@Override
public void onCreate() {
super.onCreate();
sHandler = new Handler();
}
public static Handler getHandler(){
return sHandler;
}
一个UILoader封装类
public abstract class UILoader extends FrameLayout {
private View loadingView, successView, netErrorView, emptyView;
private onRetryClickListener mOnRetryClickListener = null;
public enum UIStatus {
LOADING, SUCCESS, NETWORK_ERROR, EMPTY, NONE;
}
public UIStatus mCurrentStatus = UIStatus.NONE;
//保证了只有唯一的入口
public UILoader(@NonNull Context context) {
this(context, null);
}
public UILoader(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public UILoader(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
//初始化UI
private void init() {
SwitchUIByCurrentStatus();
}
public void updateStatus(UIStatus uiStatus){
mCurrentStatus = uiStatus;
//更新要在主线程
BaseApplication.getHandler().post(new Runnable() {
@Override
public void run() {
SwitchUIByCurrentStatus();
}
});
}
private void SwitchUIByCurrentStatus() {
//加载中
if (loadingView == null) {
loadingView = getLoadingView();
addView(loadingView);
}
//设置是否可见
loadingView.setVisibility(mCurrentStatus == UIStatus.LOADING ? VISIBLE : GONE);
//成功
if (successView == null) {
successView = getSuccessView(this);
addView(successView);
}
successView.setVisibility(mCurrentStatus == UIStatus.SUCCESS ? VISIBLE : GONE);
//网络错误
if (netErrorView == null) {
netErrorView = getNetworkErrorView();
addView(netErrorView);
}
netErrorView.setVisibility(mCurrentStatus == UIStatus.NETWORK_ERROR ? VISIBLE : GONE);
//空页面
if (emptyView == null) {
emptyView = getEmptyView();
addView(emptyView);
}
emptyView.setVisibility(mCurrentStatus == UIStatus.EMPTY ? VISIBLE : GONE);
}
protected View getEmptyView() {
return LayoutInflater.from(getContext()).inflate(R.layout.fragment_empty_view, this, false);
}
protected View getNetworkErrorView() {
View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_error_view, this, false);
view.findViewById(R.id.network_error_icon).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//todo:重新获取数据
if (mOnRetryClickListener != null) {
mOnRetryClickListener.onRetryClick();
}
}
});
return view;
}
protected abstract View getSuccessView(ViewGroup container);
private View getLoadingView() {
return LayoutInflater.from(getContext()).inflate(R.layout.fragment_loading_view, this, false);
}
//再次点击重新加载
public void setonRetryClickListener(onRetryClickListener listener){
this.mOnRetryClickListener = listener;
}
public interface onRetryClickListener{
void onRetryClick();
}
}
这里面的有三个界面是写死的,所以自己写出界面到时候就会显示出来
其中的正在加载中的界面https://blog.csdn.net/qq873044564/article/details/105614552
怎么使用
举栗说明,一个详情界面,成功时显示Recyclerview
- 这里的fl_main是一个用来显示界面的FrameLayout
private void initUiLoader() {
if (mUILoader == null) {
mUILoader = new UILoader(mContext) {
@Override
protected View getSuccessView(ViewGroup container) {
return createSuccessView(container);
}
};
}
//mFrameLayout.removeAllViews();
if (mUILoader.getParent() != null) {
((ViewGroup) mUILoader.getParent()).removeAllViews();
}
mFrameLayout.addView(mUILoader);
//重新加载自己
mUILoader.setonRetryClickListener(this);
}
private View createSuccessView(ViewGroup container) {
//编写一个正常应该显示什么内容的布局,然后对对应布局的空间进行处理,以下是对Recyclerview
View view = LayoutInflater.from(this).inflate(R.layout.layout_success_view,container,false);
tracks_list = view.findViewById(R.id.tracks_list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
tracks_list.setLayoutManager(linearLayoutManager);
tracksListAdapter = new TracksListAdapter();
tracksListAdapter.setonTrackClickListener(this);
tracks_list.setAdapter(tracksListAdapter);
tracks_list.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
outRect.bottom = UIUtil.dip2px(view.getContext(), 2);
outRect.top = UIUtil.dip2px(view.getContext(), 2);
outRect.left = UIUtil.dip2px(view.getContext(), 5);
outRect.right = UIUtil.dip2px(view.getContext(), 5);
}
});
return view;
}
在网络错误对应的地方加上
mUILoader.updateStatus(UILoader.UIStatus.NETWORK_ERROR);
在数据加载完成之前加上
if (uiLoader != null) {
mUiLoader.updateStatus(UILoader.UIStatus.LOADING);
}
在数据加载成功地方加上
if (mUILoader != null) {
mUILoader.updateStatus(UILoader.UIStatus.SUCCESS);
}
再次点击重新加载数据
@Override
public void onRetryClick() {
//重新加载数据
}
本文介绍了Android中用于处理布局加载的四种状态——正在加载、加载成功、网络错误和内容为空,并强调了创建可复用的UILoader类的重要性,以避免重复代码。通过示例展示了如何在详情界面中使用UILoader,特别是在处理网络错误和数据加载时机的场景下。
544

被折叠的 条评论
为什么被折叠?



