使用CustomStateLayout作为底层布局,可一个方法切换 加载中、加载失败、加载错误、内容为空、正常布局等等的状态;
public class CustomStateLayout extends RelativeLayout {
private Context context;
private final int NORMAL = 0; //常规状态
private final int LOADING = 1; //加载中状态
private final int LOADFAIL = 2; //加载失败状态
private final int LOADERROR = 3; //加载错误状态
private final int LOADEMPTY = 4; //内容为空状态
public CustomStateLayout(Context context) {
super(context);
this.context = context;
init();
}
public CustomStateLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
public CustomStateLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
init();
}
private RelativeLayout RelativeLayoutLoadIng,RelativeLayoutLoadFin,RelativeLayoutLoadError,RelativeLayoutLoadEmpty;
private LayoutInflater inflate;
private void init(){
inflate = LayoutInflater.from(context);
}
private void setLoadFail(){
if (RelativeLayoutLoadFin == null){
View v = this.inflate.inflate(R.layout.custom_loadfail,null);
RelativeLayoutLoadFin = v.findViewById(R.id.loadFin);
this.addView(RelativeLayoutLoadFin,new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
}else{
RelativeLayoutLoadFin.setVisibility(VISIBLE);
}
}
private void setLoadIng(){
if (RelativeLayoutLoadIng == null){
View v = this.inflate.inflate(R.layout.custom_loading,null);
RelativeLayoutLoadIng = v.findViewById(R.id.loadIng);
this.addView(RelativeLayoutLoadIng,new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
}else{
RelativeLayoutLoadIng.setVisibility(VISIBLE);
}
}
private void setLoadError(){
if (RelativeLayoutLoadError == null){
View v = this.inflate.inflate(R.layout.custom_loaderror,null);
RelativeLayoutLoadError = v.findViewById(R.id.loadError);
this.addView(RelativeLayoutLoadError,new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
}else{
RelativeLayoutLoadError.setVisibility(VISIBLE);
}
}
private void setLoadEmpty(){
if (RelativeLayoutLoadEmpty == null){
View v = this.inflate.inflate(R.layout.custom_loadempty,null);
RelativeLayoutLoadEmpty = v.findViewById(R.id.loadEmpty);
this.addView(RelativeLayoutLoadEmpty,new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
}else{
RelativeLayoutLoadEmpty.setVisibility(VISIBLE);
}
}
private void hideLoadFail(){
if (RelativeLayoutLoadFin != null){
RelativeLayoutLoadFin.setVisibility(GONE);
}
}
private void hideLoadIng(){
if (RelativeLayoutLoadIng != null){
RelativeLayoutLoadIng.setVisibility(GONE);
}
}
private void hideLoadError(){
if (RelativeLayoutLoadError != null){
RelativeLayoutLoadError.setVisibility(GONE);
}
}
private void hideLoadEmpty(){
if (RelativeLayoutLoadEmpty != null){
RelativeLayoutLoadEmpty.setVisibility(GONE);
}
}
public void showLoadIng(){
setState(LOADING);
}
public void showLoadFail(){
setState(LOADFAIL);
}
public void showLoadError(){
setState(LOADERROR);
}
public void showLoadEmpty(){
setState(LOADEMPTY);
}
public void hideAllState(){
setState(NORMAL);
}
private void setState(int state){
switch (state){
case NORMAL :
hideLoadIng();
hideLoadFail();
hideLoadError();
hideLoadEmpty();
break;
case LOADING :
setLoadIng();
hideLoadFail();
hideLoadError();
hideLoadEmpty();
break;
case LOADFAIL :
hideLoadIng();
setLoadFail();
hideLoadError();
hideLoadEmpty();
break;
case LOADERROR:
hideLoadIng();
hideLoadFail();
setLoadError();
hideLoadEmpty();
break;
case LOADEMPTY:
hideLoadIng();
hideLoadFail();
hideLoadError();
setLoadEmpty();
break;
default:
hideLoadIng();
hideLoadFail();
hideLoadError();
hideLoadEmpty();
break;
}
}
}
随便的一个“加载中”布局 R.layout.custom_loading (其它的各种状态布局都可以自己定义):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loadIng"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
>
<RelativeLayout
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/iv"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/iv"
android:layout_marginLeft="10dp"
android:text="加载中..."
/>
</RelativeLayout>
</RelativeLayout>
使用:
public class CustomLayoutStateActivity extends AppCompatActivity implements View.OnClickListener {
private CustomStateLayout mCustomStateLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_layout_state);
mCustomStateLayout = (CustomStateLayout) findViewById(R.id.customRl);
findViewById(R.id.bt1).setOnClickListener(this);
findViewById(R.id.bt2).setOnClickListener(this);
findViewById(R.id.bt3).setOnClickListener(this);
findViewById(R.id.bt4).setOnClickListener(this);
findViewById(R.id.bt5).setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt1:
mCustomStateLayout.hideAllState();
break;
case R.id.bt2:
mCustomStateLayout.showLoadIng();
break;
case R.id.bt3:
mCustomStateLayout.showLoadFail();
break;
case R.id.bt4:
mCustomStateLayout.showLoadError();
break;
case R.id.bt5:
mCustomStateLayout.showLoadEmpty();
break;
}
}
}
<?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:orientation="vertical"
android:gravity="center_horizontal">
<choi.ccb.com.morestatelayout.CustomStateLayout
android:layout_width="match_parent"
android:id="@+id/customRl"
android:layout_height="200dp">
<TextView
android:background="#ffdd88"
android:text="我是原布局"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</choi.ccb.com.morestatelayout.CustomStateLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt1"
android:text="正常的"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt2"
android:text="加载中的"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt3"
android:text="加载失败的"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt4"
android:text="加载错误的"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt5"
android:text="加载空的"
/>
</LinearLayout>