自定义一个空页面

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

/**
 * Created by ${徐嘉健} 
 */

public class EmptyView extends RelativeLayout {

    private Context mContext;
    private View inflate;
    private Button btn_empty_reload;
    private Button btn_netempty;

    public EmptyView(Context context) {
        super(context);
    }

    public EmptyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    //设置 空页面  类型
    public EmptyView setType(int type) {
        if (type == 0)//没有网络
            LayoutInflater.from(mContext).inflate(R.layout.view_network_empty, this);
        else //没有数据
            LayoutInflater.from(mContext).inflate(R.layout.view_empty, this);
        return this;
    }


    //初始化空页面 传进来的View隐藏     view是不需要加载的控件
    public EmptyView initEmpty(View view) {
        if (NetworkUtil.getNetWorkState(mContext) == -1) {//无网络状态
            inflate = LayoutInflater.from(mContext).inflate(R.layout.view_network_empty, this);
            btn_netempty = findViewById(R.id.btn_netempty);
            btn_netempty.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext, "哎呀~ 网络还没有准备好!", Toast.LENGTH_SHORT).show();
                }
            });
        } else {//空数据状态
            inflate = LayoutInflater.from(mContext).inflate(R.layout.view_empty, this);
            btn_empty_reload = findViewById(R.id.btn_empty_reload);
        }
        view.setVisibility(GONE);
        return this;
    }


    //重新刷新
    public EmptyView setOnAgainRefrush(final OnEmptyListener listener) {
        btn_empty_reload.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.setAgainRefrush();
            }
        });
        return this;
    }

}

interface OnEmptyListener {
    void setAgainRefrush();
}



布局view_network_empty



    

    

    

 

布局 view_empty




    

    
    

 

非常简单的用法 文字图片根据需求进行修改吧  暂时未处理 、

 

 

 

 

 

 

 

你可能感兴趣的:(自定义一个空页面)