加载中,加载错误,加载数据为空自定义控件

public class LoadStatusView extends FrameLayout implements View.OnClickListener {
    public static int STATUS_LOADING = 1;
    public static int STATUS_NO_NET = 2;
    public static int STATUS_FAIL_REFRESH = 3;
    public static int STATUS_HIDE = 4;

    private ProgressBar ivLoadStatus;
    private TextView tvLoadStatus;
    private AnimationDrawable loadAnimationDrawable;
    private LinearLayout nulllayout;
    private LinearLayout loadlayout;
    private LinearLayout netlayout;
    private Button anew;

    public LoadStatusView(Context context) {
        this(context, null);
    }

    public LoadStatusView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadStatusView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        View.inflate(getContext(), R.layout.load_status, this);
        setClickable(true);//这个是为了不让点击穿透到下层界面去,以免下层的控件看不见却莫名其妙的响应了
        nulllayout = findViewById(R.id.null_layout);   //内容为空时的布局
        loadlayout = findViewById(R.id.load_layout);   //加载中的布局
        netlayout = findViewById(R.id.net_layout);     //无网络或者加载错误时的布局
        anew = findViewById(R.id.anew);

    }

    public void setLoading(){
        setVisibility(View.VISIBLE);
        nulllayout.setVisibility(GONE);
        loadlayout.setVisibility(VISIBLE);
        netlayout.setVisibility(GONE);
    }

    public void setNull(){
        setVisibility(View.VISIBLE);
        nulllayout.setVisibility(VISIBLE);
        loadlayout.setVisibility(GONE);
        netlayout.setVisibility(GONE);
    }


    public void setFailRefresh(){
        setVisibility(View.VISIBLE);
        nulllayout.setVisibility(GONE);
        loadlayout.setVisibility(GONE);
        netlayout.setVisibility(VISIBLE);
        anew.setOnClickListener(this);
    }

    public void setHide(){
        setVisibility(View.GONE);

    }

   //重新加载的点击事件
    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.anew:
                onRefreshListener.onRefreshListener();
                break;
        }
    }

    /**
     * 设置点击刷新监听
     * @param listener listener
     */
    public void setOnRefreshListener(OnRefreshListener listener) {
        onRefreshListener = listener;
    }


    /**
     * 这个是打开网络设置的方法,现在都没APP这样直接打开了,不要也行
     */
    private void openSettings(){
        Intent intent = new Intent();
        intent.setAction(android.provider.Settings.ACTION_SETTINGS);
        getContext().startActivity(intent);
    }

    OnRefreshListener onRefreshListener = null;

    public interface OnRefreshListener{
        void onRefreshListener();
    }

//接下来是布局文件,大家可根据自己要求的样式进行更改

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ffffff">

<LinearLayout
    android:id="@+id/load_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    android:visibility="gone"
    >
    <ProgressBar
        android:id="@+id/ivLoadStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Small"
        />
    <TextView
        android:id="@+id/tv_load_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:paddingStart="5dp"
        android:paddingEnd="5dp"
        android:textColor="#000000"
        android:maxLines="1"
        android:ellipsize="end"
        android:text="@string/load_status_loading"/>
</LinearLayout>
    //内容为空时显示的布局
    <LinearLayout
        android:id="@+id/null_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        android:visibility="gone"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="目前没有内容"
            android:textSize="20dp"
            />
    </LinearLayout>

    //无网络或加载错误时显示的布局
    <LinearLayout
        android:id="@+id/net_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        android:visibility="gone"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="无网络或加载错误"
            />
        <Button
            android:id="@+id/anew"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="重新加载"
            />
    </LinearLayout>
</RelativeLayout>

我们把这个自定义控件放到显示数据的位置上,利用相对布局充满屏幕

使用

获取我们的控件

LoadStatusView lsv = layout.findViewById(R.id.lsv);

然后在自己的思路上进行合适的调用

包括 lsv.setNull(); lsv.setLoading(); lsv.setFailRefresh();

当然肯定有消失的方法,那就是lsv.setHide();

好了,整体内容就这些,拜~

猜你喜欢

转载自blog.csdn.net/ZhangXuxiaoqingnian/article/details/81114532