先放下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包裹起来
android:id="@+id/statusLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="我是内容"
/>
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;
}
};
}
});
}
}