Android 自定义View LoadingView 加载提示控件

使用自定义view,来实现一个加载控件,提供加载中、加载成功、加载失败、数据为空等。

public class LoadingView extends FrameLayout {
    public Context mContext;
    //加载中
    private View loadingView;
    //加载失败
    private View errorView;
    //数据为空
    private View emptyView;
    //加载成功之后需要显示的view,即子view
    private View successView;
    //重新加载按钮
    private ImageView loading_retry;

    private LoadingListener mLoadingListener;


    public LoadingView(@NonNull Context context) {
        super(context);
        mContext = context;
    }

    public LoadingView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public LoadingView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }

}

 你可以使用我定义的布局,也可以使用你自己的布局。拿errorView来说:

 

//使用原始errorview
loadingView.loadError();

 

    //使用自己想显示的错误界面
    View errorView= LayoutInflater.from(this).inflate(R.layout.error_test,null);
   loadingView.loadError(errorView);

 注意,此自定义view下只允许存在一个子view!

@Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        loadingView = inflate(mContext,R.layout.loading_load,null);
        errorView = inflate(mContext,R.layout.loading_error,null);
        emptyView = inflate(mContext,R.layout.loading_empty,null);

        this.addView(loadingView,0);
        this.addView(errorView,1);
        this.addView(emptyView,2);

        if (getChildCount() > 4){
            throw new RuntimeException("LoadingView can only have one child view. Please design your page in the child view");
        }

        emptyView.setVisibility(GONE);
        errorView.setVisibility(GONE);

        successView = getChildAt(3);
        if (successView != null){
            successView.setVisibility(GONE);
        }

        initView();
    }

 

你可能感兴趣的:(Android,安卓)