安卓中布局加载的四种状态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

  • 这里的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);
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章