自定義頁面加載LoadingLayout

做項目當中總是遇到頁面數據加載的情況,自定義了幾個加載情況的xml佈局,例如加載失敗,加載數據爲空,加載成功,正在加載等,但是發現每次都需要根據加載情況去處理顯示哪種xml,很麻煩,也很容易出錯,所以我就想以自定義組合控件的方式來處理,達到複用的目的,代碼可優化的地方有很多,大家可以根據自己的需求做修改。
首先自定義屬性了:

    <!--loadinglayout-->
    <declare-styleable name="LoadingLayout">
        <attr name="loadingLayoutId" format="reference"/>
        <attr name="failureLayoutId" format="reference"/>
        <attr name="emptyLayoutId" format="reference"/>
        <attr name="contentId" format="reference"/>
    </declare-styleable>

代碼很簡單,自己看看

public class LoadingLayout extends FrameLayout {


    private View loadingView, failureView, emptyView, contentView;

    private RetryListener retryListener;

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

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

    public LoadingLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LoadingLayout, defStyleAttr, 0);

        int loadingId = typedArray.getResourceId(R.styleable.LoadingLayout_loadingLayoutId, 0);

        int failureId = typedArray.getResourceId(R.styleable.LoadingLayout_failureLayoutId, 0);

        int emptyId = typedArray.getResourceId(R.styleable.LoadingLayout_emptyLayoutId, 0);

        int contentId = typedArray.getResourceId(R.styleable.LoadingLayout_contentId, 0);

        typedArray.recycle();

        initView(context, loadingId, failureId, emptyId, contentId);

    }

    //初始化三個狀態View
    private void initView(Context context, int loadingId, int failureId, int emptyId, int contentId) {

        createChildView(context, loadingId);
        createChildView(context, failureId);
        createChildView(context, emptyId);
        createChildView(context, contentId);
    }


    //創建子視圖
    private void createChildView(Context context, int Id) {

        if (Id!=0) {

            View.inflate(context,Id, this);
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        //獲取顯示內容區域
        if (getChildCount() > 3) {
            loadingView = getChildAt(0);
            failureView = getChildAt(1);
            emptyView = getChildAt(2);
            contentView = getChildAt(3);
            contentView.setVisibility(GONE);
            if (loadingView != null) {

                loadingView.setVisibility(GONE);
            }

            if (failureView != null) {

                failureView.setVisibility(GONE);
                View rButton = (failureView.findViewById(R.id.tv_retry) == null) ? failureView : failureView.findViewById(R.id.tv_retry);
                rButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (retryListener != null) {


                            retryListener.retryClick();


                        }
                    }
                });
            }

            if (emptyView != null) {

                emptyView.setVisibility(GONE);
            }

        }


    }


    //設置重試監聽
    public void setRetryListener(RetryListener retryListener) {
        this.retryListener = retryListener;
    }

    //顯示加載view
    public void showLoading() {

        show(1);

    }


    //顯示失敗view
    public void showFailure() {

        show(2);

    }


    //顯示空view
    public void showEmpty() {

        show(3);

    }


    //顯示空view
    public void showContent() {

        show(4);
    }


    //根據id展示佈局
    private void show(int id) {

        if (loadingView != null) {

            loadingView.setVisibility(id == 1 ? VISIBLE : GONE);
        }

        if (failureView != null) {

            failureView.setVisibility(id == 2 ? VISIBLE : GONE);

        }

        if (emptyView != null) {

            emptyView.setVisibility(id == 3 ? VISIBLE : GONE);
        }

        if (contentView != null) {

            contentView.setVisibility(id == 4 ? VISIBLE : GONE);
        }
    }


    public interface RetryListener {

        void retryClick();

    }

}

然後就是如何使用了

<?xml version="1.0" encoding="utf-8"?>
<com.yjjy.app.view.LoadingLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/loadingLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:loadingLayoutId="@layout/progressdialoag"
    app:failureLayoutId="@layout/loading_failure"
    <!--可以在這裏引用設置contentlayout-->
    >
        <!--也可以在這裏引用設置contentlayout-->
</com.yjjy.app.view.LoadingLayout>

然後在使用的地方調用下面幾個方法即可:

    //顯示加載view
    public void showLoading() {

        show(1);

    }


    //顯示失敗view
    public void showFailure() {

        show(2);

    }


    //顯示空view
    public void showEmpty() {

        show(3);

    }


    //顯示空view
    public void showContent() {

        show(4);
    }

程序員內功修煉手冊 不定期分享程序員基礎知識,大前端知識!想跟博主一塊成長的快快關注吧!

在這裏插入圖片描述

發佈了87 篇原創文章 · 獲贊 126 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章