四種狀態:正在加載中,加載成功,網絡錯誤,內容爲空
所以寫一個可以複用的類很重要,避免重複寫代碼
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
- 這裏的detail_list是一個用來顯示界面的FrameLayout
private void initView() {
detail_list = this.findViewById(R.id.detail_list);
if (uiLoader == null) {
uiLoader = new UILoader(this) {
@Override
protected View getSuccessView(ViewGroup container) {
return createSuccessView(container);
}
};
}
detail_list.removeAllViews();
detail_list.addView(uiLoader);
uiLoader.setonRetryClickListener(DetailActivity.this);
}
private View createSuccessView(ViewGroup container) {
View view = LayoutInflater.from(this).inflate(R.layout.item_succ_tracks_list,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;
}
@Override
public void onNetworkError(int i, String s) {
LogUtil.e(TAG, "onNetworkError" + i + s);
if (uiLoader != null) {
uiLoader.updateStatus(UILoader.UIStatus.NETWORK_ERROR);
}
}
@Override
public void onLoading() {
if (uiLoader != null) {
uiLoader.updateStatus(UILoader.UIStatus.LOADING);
}
}
//重新加載數據
@Override
public void onRetryClick() {
if (albumDetailPresenter != null) {
albumDetailPresenter.getAlbumDetail(currentAlbum.getId(),1);
}
}