StatusLayout佈局又稱爲多狀態佈局包含(空佈局,加載中,加載失敗,網絡錯誤4種狀態),此佈局十分靈活

介紹下寫這個佈局的背景

之前一直用鴻洋大神公衆號推薦文章裏的StatusLayoutManager是在java的的的的代碼用構建的方式來創建,具體是哪篇文章,實在抱歉沒有翻閱到。貼下代碼

 mStatusLayoutManager = StatusLayoutManager.newBuilder(this)
                .contentView(layoutResID)
                .loadingView(R.layout.view_loading)
                .emptyDataView(R.layout.view_empty_data)
                .netWorkErrorView(R.layout.view_network_error)
                .errorView(R.layout.view_load_error)
                .retryViewId(R.id.tv_try)
                .onRetryListener(new OnRetryListener() {
                    @Override
                    public void onRetry() {
                        mStatusLayoutManager.showLoading();
                        onReTry();
                    }
                })
                .build();

十分麻煩,所以就有的我的StatusLayout,可控性更強更加靈活。

先放下StatusLayout效果

1.默認設置的四種狀態

 

2.自定義佈局以網絡錯誤爲例

3.自定義全局狀態佈局樣式

如何使用

A.添加倉庫地址

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url  "https://dl.bintray.com/yemao02/support"
        }
    }
}

B.添加庫

implementation 'yemao.support:widget:1.0.0@aar'

1.在需要狀態佈局的佈局用StatusLayout包裹起來

    <com.ye.widget.StatusLayout
        android:id="@+id/statusLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="我是內容"
        />
    </com.ye.widget.StatusLayout>

StatusLayout中5個方法分別爲。

showEmpty()//顯示空佈局
showLoading()//顯示加載中佈局
showLoadError()//顯示加載失敗佈局
showNetworkError()//顯示網絡錯誤佈局
showContent()//顯示主佈局

觸發各個狀態佈局

高級功能

。自定義各個狀態佈局:需繼承自BasicStatusLayout

例如:

 mStatusLayout!!.setNetworkError(object : BasicStatusLayout(this@MainActivity) {
                override fun getLayoutId(): Int {
                    return R.layout.view_layout_my_network
                }

                override fun getRetryId(): Int {
                    return R.id.btn_retry
                }
            })

BasicStatusLayout介紹:

繼承自的的的的的LinearLayout裏面有兩個方法需實現 

@LayoutRes int getLayoutId();//頁面佈局Id
@IdRes int getRetryId();//頁面重試按鈕id

。重試按鈕監聽

  mStatusLayout.setOnRetryListener(new StatusLayout.OnRetryListener() {
            @Override
            public void onRetry() {
                
            }
        });

 

。設置全局StatusLayout(在應用中設置四種狀態空佈局,加載中,加載失敗,網絡錯誤,當某個地方使用StatusLayout時如果沒有主動設置某個狀態的佈局時,默認使用全局的佈局樣式

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        StatusLayout.setDefaultStatusCreator(new DefaultStatusCreator() {
            @Override
            public BasicStatusLayout emptyLayout() {
                return new BasicStatusLayout(getApplicationContext()) {
                    @Override
                    public int getLayoutId() {
                        return R.layout.view_layout_default_empty;
                    }

                    @Override
                    public int getRetryId() {
                        return 0;
                    }
                };
            }

            @Override
            public BasicStatusLayout loadingLayout() {
                return new BasicStatusLayout(getApplicationContext()) {
                    @Override
                    public int getLayoutId() {
                        return R.layout.view_layout_default_loading;
                    }

                    @Override
                    public int getRetryId() {
                        return 0;
                    }
                };
            }

            @Override
            public BasicStatusLayout loadErrorLayout() {
                return new BasicStatusLayout(getApplicationContext()) {
                    @Override
                    public int getLayoutId() {
                        return R.layout.view_layout_default_load_error;
                    }

                    @Override
                    public int getRetryId() {
                        return R.id.btn_retry;
                    }
                };
            }

            @Override
            public BasicStatusLayout networkErrorLayout() {
                return new BasicStatusLayout(getApplicationContext()) {
                    @Override
                    public int getLayoutId() {
                        return R.layout.view_layout_default_network;
                    }

                    @Override
                    public int getRetryId() {
                        return R.id.btn_retry;
                    }
                };
            }
        });
    }
}

項目示例地址:https://gitee.com/yrmao/StatusLayoutSimple-android

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章