頁(yè)面加載狀態(tài)布局

分享一個(gè)頁(yè)面加載狀態(tài)布局

public class LyLoadingLayout extends FrameLayout {

    public final static int Success = 0;
    public final static int Empty = 1;
    public final static int Error = 2;
    public final static int No_Network = 3;
    public final static int Loading = 4;
    private int mState;

    private Context mContext;
    private View loadingPage;
    private View errorPage;
    private View emptyPage;
    private View networkPage;
    private View defineLoadingPage;

    private ImageView errorImg;
    private ImageView emptyImg;
    private ImageView networkImg;

    private TextView errorText;
    private TextView emptyText;
    private TextView networkText;

    private TextView errorReloadBtn;
    private TextView networkReloadBtn;

    private View contentView;
    private OnReloadListener listener;
    //是否一開(kāi)始顯示contentview今妄,默認(rèn)不顯示
    private boolean isFirstVisible;

    //配置
    private static Config mConfig = new Config();
    private static String emptyStr = "暫無(wú)數(shù)據(jù)";
    private static String errorStr = "請(qǐng)求錯(cuò)誤谱轨,請(qǐng)重試";
    private static String netwrokStr = "找不到網(wǎng)絡(luò)";
    private static String reloadBtnStr = "點(diǎn)擊刷新";
    private static int emptyImgId = R.drawable.empty_com;
    private static int errorImgId = R.drawable.empty_com;
    private static int networkImgId = R.drawable.default_no_network;
    private static int reloadBtnId = R.drawable.verify_shape_main;
    private static int tipTextSize = 14;
    private static int buttonTextSize = 14;
    private static int tipTextColor = 0xff999999;
    private static int buttonTextColor = 0xff999999;
    private static int buttonWidth = -1;
    private static int buttonHeight = -1;
    private static int loadingLayoutId = R.layout.widget_loading_page;

    public LyLoadingLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LoadingLayout);
        isFirstVisible = a.getBoolean(R.styleable.LoadingLayout_isFirstVisible, false);
        a.recycle();
    }

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

    public LyLoadingLayout(Context context) {
        super(context);
        this.mContext = context;
    }

    /**
     * contentView 當(dāng)加載完成xml后執(zhí)行
     */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (getChildCount() > 1) {
            throw new IllegalStateException("LyLoadingLayout can host only one direct child");
        }
        contentView = this.getChildAt(0);
        if (!isFirstVisible) {
            contentView.setVisibility(View.GONE);
        }
        build();
    }

    private void build() {

        loadingPage = LayoutInflater.from(mContext).inflate(loadingLayoutId, null);
        errorPage = LayoutInflater.from(mContext).inflate(R.layout.widget_error_page, null);
        emptyPage = LayoutInflater.from(mContext).inflate(R.layout.widget_empty_page, null);
        networkPage = LayoutInflater.from(mContext).inflate(R.layout.widget_nonetwork_page, null);
        defineLoadingPage = null;
        networkPage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        netwrokStr = "重新加載";


        errorText = findViewById(errorPage, R.id.error_text);
        emptyText = findViewById(emptyPage, R.id.empty_text);
        networkText = findViewById(networkPage, R.id.no_network_text);

        errorImg = findViewById(errorPage, R.id.error_img);
        emptyImg = findViewById(emptyPage, R.id.empty_img);
        networkImg = findViewById(networkPage, R.id.no_network_img);

        errorReloadBtn = findViewById(errorPage, R.id.error_reload_btn);
        networkReloadBtn = findViewById(networkPage, R.id.no_network_reload_btn);

        errorReloadBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (listener != null) {
                    listener.onReload(v);
                }
            }
        });
        networkReloadBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (listener != null) {
                    listener.onReload(v);
                }
            }
        });

        errorText.setText(errorStr);
        emptyText.setText(emptyStr);
        networkText.setText(netwrokStr);

        errorText.setTextSize(tipTextSize);
        emptyText.setTextSize(tipTextSize);
        networkText.setTextSize(tipTextSize);

        errorText.setTextColor(ContextCompat.getColor(mContext, tipTextColor));
        emptyText.setTextColor(ContextCompat.getColor(mContext, tipTextColor));
        networkText.setTextColor(ContextCompat.getColor(mContext, tipTextColor));

        errorImg.setImageResource(errorImgId);
        emptyImg.setImageResource(emptyImgId);
        networkImg.setImageResource(networkImgId);


        errorReloadBtn.setBackgroundResource(reloadBtnId);
        networkReloadBtn.setBackgroundResource(reloadBtnId);

        errorReloadBtn.setText(reloadBtnStr);
        networkReloadBtn.setText(reloadBtnStr);

        errorReloadBtn.setTextSize(buttonTextSize);
        networkReloadBtn.setTextSize(buttonTextSize);

        errorReloadBtn.setTextColor(ContextCompat.getColor(mContext, buttonTextColor));
        networkReloadBtn.setTextColor(ContextCompat.getColor(mContext, buttonTextColor));

        if (buttonHeight != -1) {

            errorReloadBtn.setHeight(dp2px(mContext, buttonHeight));
            networkReloadBtn.setHeight(dp2px(mContext, buttonHeight));
        }
        if (buttonWidth != -1) {

            errorReloadBtn.setWidth(dp2px(mContext, buttonWidth));
            networkReloadBtn.setWidth(dp2px(mContext, buttonWidth));
        }

        this.addView(networkPage);
        this.addView(emptyPage);
        this.addView(errorPage);
        this.addView(loadingPage);
    }

    /**
     * 改變視圖加載狀態(tài)  需要手動(dòng)觸發(fā)
     *
     * @param state
     * @param mLyLoadingLayout
     */
    public void setLoadViewState(int state, LyLoadingLayout mLyLoadingLayout) {
        if (mLyLoadingLayout == null) return;
        if (state != mLyLoadingLayout.getStatus()) {
            mLyLoadingLayout.setStatus(state);
        }
    }

    /**
     * 設(shè)置當(dāng)天狀態(tài)
     *
     * @param status Success:加載成功;Loading:加載中;
     *               Empty:數(shù)據(jù)為空;Error:加載失敗
     *               No_Network:無(wú)網(wǎng)絡(luò)
     */
    public void setStatus(@Flavour int status) {

        this.mState = status;

        switch (status) {
            case Success:
                animateVisible(contentView);
                emptyPage.setVisibility(View.GONE);
                errorPage.setVisibility(View.GONE);
                networkPage.setVisibility(View.GONE);
                if (defineLoadingPage != null) {
                    animateGone(defineLoadingPage);
                } else {
                    animateGone(loadingPage);
                }
                break;

            case Loading:
                contentView.setVisibility(View.GONE);
                emptyPage.setVisibility(View.GONE);
                errorPage.setVisibility(View.GONE);
                networkPage.setVisibility(View.GONE);
                if (defineLoadingPage != null) {
                    animateVisible(defineLoadingPage);
                } else {
                    animateVisible(loadingPage);
                }
                break;

            case Empty:
                contentView.setVisibility(View.GONE);
                animateVisible(emptyPage);
                errorPage.setVisibility(View.GONE);
                networkPage.setVisibility(View.GONE);
                if (defineLoadingPage != null) {
                    animateGone(defineLoadingPage);
                } else {
                    animateGone(loadingPage);
                }
                break;

            case Error:
                contentView.setVisibility(View.GONE);
                emptyPage.setVisibility(View.GONE);
                animateVisible(errorPage);
                networkPage.setVisibility(View.GONE);
                if (defineLoadingPage != null) {
                    animateGone(defineLoadingPage);
                } else {
                    animateGone(loadingPage);
                }
                break;

            case No_Network:
                contentView.setVisibility(View.GONE);
                emptyPage.setVisibility(View.GONE);
                errorPage.setVisibility(View.GONE);
                animateVisible(networkPage);
                if (defineLoadingPage != null) {
                    animateGone(defineLoadingPage);
                } else {
                    animateGone(loadingPage);
                }
                break;

            default:
                break;
        }

    }

    /**
     * View淡入
     */
    public void animateVisible(View view) {
        if (view == null) return;
        view.setAlpha(0f);
        view.setVisibility(View.VISIBLE);
        view.animate().alpha(1f).setDuration(450).setListener(null);
    }

    /**
     * View 淡出
     */
    public void animateGone(final View view) {
        if (view == null) return;
        view.animate().alpha(0f).setDuration(400).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.GONE);
            }
        });
    }


    /**
     * 返回當(dāng)前狀態(tài){Success, Empty, Error, No_Network, Loading}
     *
     * @return
     */
    public int getStatus() {

        return mState;
    }

    /**
     * 設(shè)置Empty狀態(tài)提示文本,僅對(duì)當(dāng)前所在的地方有效
     *
     * @param text
     * @return
     */
    public LyLoadingLayout setEmptyText(String text) {

        emptyText.setText(text);
        return this;
    }

    /**
     * 設(shè)置Error狀態(tài)提示文本,僅對(duì)當(dāng)前所在的地方有效
     *
     * @param text
     * @return
     */
    public LyLoadingLayout setErrorText(String text) {
        errorText.setText(text);
        return this;
    }

    /**
     * 設(shè)置No_Network狀態(tài)提示文本场钉,僅對(duì)當(dāng)前所在的地方有效
     *
     * @param text
     * @return
     */
    public LyLoadingLayout setNoNetworkText(String text) {

        networkText.setText(text);
        return this;
    }

    /**
     * 設(shè)置Empty狀態(tài)顯示圖片来吩,僅對(duì)當(dāng)前所在的地方有效
     *
     * @param id
     * @return
     */
    public LyLoadingLayout setEmptyImage(@DrawableRes int id) {


        emptyImg.setImageResource(id);
        return this;
    }

    /**
     * 設(shè)置Error狀態(tài)顯示圖片敢辩,僅對(duì)當(dāng)前所在的地方有效
     *
     * @param id
     * @return
     */
    public LyLoadingLayout setErrorImage(@DrawableRes int id) {

        errorImg.setImageResource(id);
        return this;
    }

    /**
     * 設(shè)置No_Network狀態(tài)顯示圖片,僅對(duì)當(dāng)前所在的地方有效
     *
     * @param id
     * @return
     */
    public LyLoadingLayout setNoNetworkImage(@DrawableRes int id) {

        networkImg.setImageResource(id);
        return this;
    }

    /**
     * 設(shè)置Empty狀態(tài)提示文本的字體大小弟疆,僅對(duì)當(dāng)前所在的地方有效
     *
     * @param sp
     * @return
     */
    public LyLoadingLayout setEmptyTextSize(int sp) {

        emptyText.setTextSize(sp);
        return this;
    }

    /**
     * 設(shè)置Error狀態(tài)提示文本的字體大小戚长,僅對(duì)當(dāng)前所在的地方有效
     *
     * @param sp
     * @return
     */
    public LyLoadingLayout setErrorTextSize(int sp) {

        errorText.setTextSize(sp);
        return this;
    }

    /**
     * 設(shè)置No_Network狀態(tài)提示文本的字體大小,僅對(duì)當(dāng)前所在的地方有效
     *
     * @param sp
     * @return
     */
    public LyLoadingLayout setNoNetworkTextSize(int sp) {

        networkText.setTextSize(sp);
        return this;
    }

    /**
     * 設(shè)置Empty狀態(tài)圖片的顯示與否怠苔,僅對(duì)當(dāng)前所在的地方有效
     *
     * @param bool
     * @return
     */
    public LyLoadingLayout setEmptyImageVisible(boolean bool) {

        if (bool) {
            emptyImg.setVisibility(View.VISIBLE);
        } else {
            emptyImg.setVisibility(View.GONE);
        }
        return this;
    }

    /**
     * 設(shè)置Error狀態(tài)圖片的顯示與否同廉,僅對(duì)當(dāng)前所在的地方有效
     *
     * @param bool
     * @return
     */
    public LyLoadingLayout setErrorImageVisible(boolean bool) {

        if (bool) {
            errorImg.setVisibility(View.VISIBLE);
        } else {
            errorImg.setVisibility(View.GONE);
        }
        return this;
    }

    /**
     * 設(shè)置No_Network狀態(tài)圖片的顯示與否,僅對(duì)當(dāng)前所在的地方有效
     *
     * @param bool
     * @return
     */
    public LyLoadingLayout setNoNetworkImageVisible(boolean bool) {

        if (bool) {
            networkImg.setVisibility(View.VISIBLE);
        } else {
            networkImg.setVisibility(View.GONE);
        }
        return this;
    }

    /**
     * 設(shè)置ReloadButton的文本柑司,僅對(duì)當(dāng)前所在的地方有效
     *
     * @param text
     * @return
     */
    public LyLoadingLayout setReloadButtonText(@NonNull String text) {

        errorReloadBtn.setText(text);
        networkReloadBtn.setText(text);
        return this;
    }

    /**
     * 設(shè)置ReloadButton的文本字體大小迫肖,僅對(duì)當(dāng)前所在的地方有效
     *
     * @param sp
     * @return
     */
    public LyLoadingLayout setReloadButtonTextSize(int sp) {

        errorReloadBtn.setTextSize(sp);
        networkReloadBtn.setTextSize(sp);
        return this;
    }

    /**
     * 設(shè)置ReloadButton的文本顏色,僅對(duì)當(dāng)前所在的地方有效
     *
     * @param id
     * @return
     */
    public LyLoadingLayout setReloadButtonTextColor(@ColorRes int id) {

        errorReloadBtn.setTextColor(ContextCompat.getColor(mContext, id));
        networkReloadBtn.setTextSize(ContextCompat.getColor(mContext, id));
        return this;
    }

    /**
     * 設(shè)置ReloadButton的背景攒驰,僅對(duì)當(dāng)前所在的地方有效
     *
     * @param id
     * @return
     */
    public LyLoadingLayout setReloadButtonBackgroundResource(@DrawableRes int id) {

        errorReloadBtn.setBackgroundResource(id);
        networkReloadBtn.setBackgroundResource(id);
        return this;
    }

    /**
     * 設(shè)置ReloadButton的監(jiān)聽(tīng)器
     *
     * @param listener
     * @return
     */
    public LyLoadingLayout setOnReloadListener(OnReloadListener listener) {

        this.listener = listener;
        return this;
    }

    /**
     * 自定義加載頁(yè)面蟆湖,僅對(duì)當(dāng)前所在的Activity有效
     *
     * @param view
     * @return
     */
    public LyLoadingLayout setLoadingPage(View view) {

        defineLoadingPage = view;
        this.removeView(loadingPage);
        defineLoadingPage.setVisibility(View.GONE);
        this.addView(view);
        return this;
    }

    /**
     * 自定義加載頁(yè)面,僅對(duì)當(dāng)前所在的地方有效
     *
     * @param id
     * @return
     */
    public LyLoadingLayout setLoadingPage(@LayoutRes int id) {

        this.removeView(loadingPage);
        View view = LayoutInflater.from(mContext).inflate(id, null);
        defineLoadingPage = view;
        defineLoadingPage.setVisibility(View.GONE);
        this.addView(view);
        return this;
    }

    /**
     * 獲取當(dāng)前自定義的loadingpage
     *
     * @return
     */
    public View getLoadingPage() {

        return defineLoadingPage;
    }


    /**
     * 獲取全局使用的loadingpage
     *
     * @return
     */
    public View getGlobalLoadingPage() {

        return loadingPage;
    }

    @IntDef({Success, Empty, Error, No_Network, Loading})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Flavour {

    }

    public interface OnReloadListener {

        void onReload(View v);
    }

    /**
     * 獲取全局配置的class
     *
     * @return
     */
    public static Config getConfig() {

        return mConfig;
    }

    /**
     * 全局配置的Class玻粪,對(duì)所有使用到的地方有效
     */
    public static class Config {

        public Config setErrorText(@NonNull String text) {

            errorStr = text;
            return mConfig;
        }

        public Config setEmptyText(@NonNull String text) {

            emptyStr = text;
            return mConfig;
        }

        public Config setNoNetworkText(@NonNull String text) {

            netwrokStr = text;
            return mConfig;
        }

        public Config setReloadButtonText(@NonNull String text) {

            reloadBtnStr = text;
            return mConfig;
        }

        /**
         * 設(shè)置所有提示文本的字體大小
         *
         * @param sp
         * @return
         */
        public Config setAllTipTextSize(int sp) {

            tipTextSize = sp;
            return mConfig;
        }

        /**
         * 設(shè)置所有提示文本的字體顏色
         *
         * @param color
         * @return
         */
        public Config setAllTipTextColor(@ColorRes int color) {

            tipTextColor = color;
            return mConfig;
        }

        public Config setReloadButtonTextSize(int sp) {

            buttonTextSize = sp;
            return mConfig;
        }

        public Config setReloadButtonTextColor(@ColorRes int color) {

            buttonTextColor = color;
            return mConfig;
        }

        public Config setReloadButtonBackgroundResource(@DrawableRes int id) {

            reloadBtnId = id;
            return mConfig;
        }

        public Config setReloadButtonWidthAndHeight(int width_dp, int height_dp) {

            buttonWidth = width_dp;
            buttonHeight = height_dp;
            return mConfig;
        }

        public Config setErrorImage(@DrawableRes int id) {

            errorImgId = id;
            return mConfig;
        }

        public Config setEmptyImage(@DrawableRes int id) {

            emptyImgId = id;
            return this;
        }

        public Config setNoNetworkImage(@DrawableRes int id) {

            networkImgId = id;
            return mConfig;
        }

        public Config setLoadingPageLayout(@LayoutRes int id) {

            loadingLayoutId = id;
            return mConfig;
        }

    }

    public int dp2px(Context context, int dip) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f);
    }

    public <T extends View> T findViewById(View v, int id) {
        return (T) v.findViewById(id);
    }
}

布局文件

widget_error_page.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:visibility="gone">

    <ImageView
        android:id="@+id/error_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/empty_com" />

    <TextView
        android:id="@+id/error_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:layout_marginTop="10dp"
        android:textColor="#999999" />

    <TextView
        android:id="@+id/error_reload_btn"
        android:layout_width="140dp"
        android:layout_height="36dp"
        android:clickable="true"
        android:gravity="center"
        android:text="重新加載"
        android:textSize="14sp"
        android:textColor="#999999" />


</LinearLayout>
widget_loading_page.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:gravity="center"
    android:orientation="horizontal"
    android:visibility="gone">

    <ProgressBar
        style="@style/Widget.AppCompat.ProgressBar"
        android:indeterminateDuration="1000"
        android:indeterminateOnly="true"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:indeterminateDrawable="@drawable/content_loading"
        />

</LinearLayout>
widget_nonetwork_page.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mLlNoNetwork"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:visibility="gone">

    <ImageView
        android:id="@+id/no_network_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/default_no_network" />

    <TextView
        android:id="@+id/no_network_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:textSize="14sp"
        android:layout_marginTop="10dp"
        android:textColor="#999999"
        tools:text="網(wǎng)絡(luò)錯(cuò)誤" />

    <TextView
        android:id="@+id/no_network_reload_btn"
        android:layout_width="140dp"
        android:layout_height="36dp"
        android:clickable="true"
        android:gravity="center"
        android:text="重新加載"
        android:textSize="14sp"
        android:textColor="#999999" />


</LinearLayout>
widget_empty_page.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:visibility="gone"
    >

    <ImageView
        android:id="@+id/empty_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/empty_com"
        />

    <TextView
        android:id="@+id/empty_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:lineSpacingExtra="4dp"
        android:textSize="14sp"
        android:textColor="#999999"
        />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="LoadingLayout">
        <attr name="isFirstVisible" format="boolean" />
    </declare-styleable>

</resources>

下面介紹用法

LoadingLayout支持全局配置隅津,對(duì)所有使用到的地方都起效,需要在Application中配置劲室,如下:

public class App extends Application {

   @Override
   public void onCreate() {
     super.onCreate();
     LyLoadingLayout.getConfig()
                  .setErrorText("出錯(cuò)啦~請(qǐng)稍后重試伦仍!")
                  .setEmptyText("抱歉,暫無(wú)數(shù)據(jù)")
                  .setNoNetworkText("無(wú)網(wǎng)絡(luò)連接痹籍,請(qǐng)檢查您的網(wǎng)絡(luò)···")
                  .setErrorImage(R.mipmap.define_error)
                  .setEmptyImage(R.mipmap.define_empty)
                  .setNoNetworkImage(R.mipmap.define_nonetwork)
                  .setAllTipTextColor(R.color.gray)
                  .setAllTipTextSize(14)
                  .setReloadButtonText("點(diǎn)我重試哦")
                  .setReloadButtonTextSize(14)
                  .setReloadButtonTextColor(R.color.gray)
                  .setReloadButtonWidthAndHeight(150,40);
   }
}

由于“加載中”的頁(yè)面呢铆,可能每個(gè)App都不一樣,因此蹲缠,LoadingLayout支持自定義LoadingPage棺克,如下:

LyLoadingLayout.getConfig().setLoadingPageLayout(R.layout.define_loading_page);

同時(shí),為了適應(yīng)個(gè)別界面的“特殊需求”线定,LoadingLayout也支持局部設(shè)置各種屬性娜谊,僅對(duì)當(dāng)前對(duì)象生效,不影響全局斤讥。如下:

LyLoadingLayout  loading = (LyLoadingLayout  ) findViewById(R.id.loading_layout);
loading.setLoadingPage(R.layout.define_loading_page)
       .setEmptyText("暫無(wú)報(bào)告數(shù)據(jù)")
       .setErrorText("")
       .setNoNetworkText("")
       .setErrorImage(R.mipmap.ic_launcher)
       .setErrorTextSize(16)
       .setReloadButtonText("點(diǎn)我重新加載哦"); //等等

為ReloadButton設(shè)置監(jiān)聽(tīng):

  loadingLayout.setOnReloadListener(new LyLoadingLayout.OnReloadListener() {
    @Override
    public void onReload(View v) {
      
    }
    });

設(shè)置顯示的頁(yè)面:

loadingLayout.setStatus(LyLoadingLayout.Loading);//加載中
loadingLayout.setStatus(LyLoadingLayout.Empty);//無(wú)數(shù)據(jù)
loadingLayout.setStatus(LyLoadingLayout.Error);//錯(cuò)誤
loadingLayout.setStatus(LyLoadingLayout.No_Network);//無(wú)網(wǎng)絡(luò)
loadingLayout.setStatus(LyLoadingLayout.Success);//加載成功

最后纱皆,在xml里面使用:

<LyLoadingLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent
    app:isFirstVisible="true">

<TextView
    android:background="@color/colorPrimary"
    android:visibility="visible"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="ContentView"/>

</LyLoadingLayout>

注意:

  • isFirstVisible屬性用來(lái)控制contentView一開(kāi)始是否隱藏湾趾,由于LoadingLayout原理是在xml渲染完成后在contentView上鋪上三層View,因此派草,一開(kāi)始如果不隱藏搀缠,等contentView渲染完成后調(diào)用: loadingLayout.setStatus(LoadingLayout.Loading); 會(huì)造成界面閃爍的效果,影響體驗(yàn)近迁,因此默認(rèn)將contentView隱藏艺普,所以數(shù)據(jù)加載完成后一定要調(diào)用loadingLayout.setStatus(LyLoadingLayout.Success);,將contentView顯示出來(lái)鉴竭。這樣也能解決未獲取到數(shù)據(jù)的情況下歧譬,被用戶看到雜亂無(wú)章的布局,個(gè)人還是比較喜歡默認(rèn)隱藏contentView搏存;
  • 為了方便管理瑰步,LyLoadingLayout只能有一個(gè)直屬子View,類似ScrollView璧眠,添加兩個(gè)直屬子View會(huì)拋出異常throw new IllegalStateException("LyLoadingLayout can host only one direct child");缩焦;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市责静,隨后出現(xiàn)的幾起案子舌界,更是在濱河造成了極大的恐慌,老刑警劉巖泰演,帶你破解...
    沈念sama閱讀 206,968評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件呻拌,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡睦焕,警方通過(guò)查閱死者的電腦和手機(jī)藐握,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)垃喊,“玉大人猾普,你說(shuō)我怎么就攤上這事”久眨” “怎么了初家?”我有些...
    開(kāi)封第一講書(shū)人閱讀 153,220評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)乌助。 經(jīng)常有香客問(wèn)我溜在,道長(zhǎng),這世上最難降的妖魔是什么他托? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,416評(píng)論 1 279
  • 正文 為了忘掉前任掖肋,我火速辦了婚禮,結(jié)果婚禮上赏参,老公的妹妹穿的比我還像新娘志笼。我一直安慰自己沿盅,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,425評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布纫溃。 她就那樣靜靜地躺著腰涧,像睡著了一般。 火紅的嫁衣襯著肌膚如雪紊浩。 梳的紋絲不亂的頭發(fā)上南窗,一...
    開(kāi)封第一講書(shū)人閱讀 49,144評(píng)論 1 285
  • 那天,我揣著相機(jī)與錄音郎楼,去河邊找鬼。 笑死窒悔,一個(gè)胖子當(dāng)著我的面吹牛呜袁,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播简珠,決...
    沈念sama閱讀 38,432評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼阶界,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了聋庵?” 一聲冷哼從身側(cè)響起膘融,我...
    開(kāi)封第一講書(shū)人閱讀 37,088評(píng)論 0 261
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎祭玉,沒(méi)想到半個(gè)月后氧映,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,586評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡脱货,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,028評(píng)論 2 325
  • 正文 我和宋清朗相戀三年岛都,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片振峻。...
    茶點(diǎn)故事閱讀 38,137評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡臼疫,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出扣孟,到底是詐尸還是另有隱情烫堤,我是刑警寧澤,帶...
    沈念sama閱讀 33,783評(píng)論 4 324
  • 正文 年R本政府宣布凤价,位于F島的核電站鸽斟,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏利诺。R本人自食惡果不足惜湾盗,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,343評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望立轧。 院中可真熱鬧格粪,春花似錦躏吊、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,333評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至疆导,卻和暖如春赁项,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背澈段。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,559評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工悠菜, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人败富。 一個(gè)月前我還...
    沈念sama閱讀 45,595評(píng)論 2 355
  • 正文 我出身青樓悔醋,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親兽叮。 傳聞我的和親對(duì)象是個(gè)殘疾皇子芬骄,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,901評(píng)論 2 345