Android手写状态切换布局

实现状态切换布局

Android手写状态切换布局
image

效果图

Android手写状态切换布局
image

原理

继承RelativeLayout,然后向其中添加各种状态的View,通过对各种View的显示隐藏的切换来实现各种状态的切换。

实现过程

1.继承RelativeLayout,这里通过构造方法之间的调用来简化实例化需要写的代码

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

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

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

}

2.添加设置状态View的方法,以及盛放状态View的集合

private View LoadingView,SuccessView,FaildView;
private ArrayList<View> views;
public void setStatusView(View loadingView, View successView, View faildView){
    if(views==null){
        views=new ArrayList<>();
    }else if(views.size()>0){
        views.clear();
    }
    if(getChildCount()>0){
        removeAllViews();
    }
    LoadingView=loadingView;
    SuccessView=successView;
    FaildView=faildView;
    LoadingView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    SuccessView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    FaildView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    addView(LoadingView);
    addView(SuccessView);
    addView(FaildView);
    views.add(LoadingView);
    views.add(SuccessView);
    views.add(FaildView);
}

3.添加隐藏状态View的方法

private void HideViews(){
    if(views!=null&&views.size()>0){
        for(View v:views){
            v.setVisibility(GONE);
        }
    }
}

4.添加设置状态的方法,这里使用enum来对状态进行判断

public void setStatus(LoadingType loadingType){
    //在设置之前先将所有View隐藏
    HideViews();
    switch (loadingType){
        case LOADING:
            if(LoadingView!=null){
                LoadingView.setVisibility(VISIBLE);
            }
            break;
        case SUCCESS:
            if(SuccessView!=null){
                SuccessView.setVisibility(VISIBLE);
            }
            break;
        case FAILD:
            if(FaildView!=null){
                FaildView.setVisibility(VISIBLE);
            }
            break;
    }
}
public enum LoadingType{
    LOADING,SUCCESS,FAILD
}

5.在布局文件中使用LoadingLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.jianshupro.MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="wrap_content">
    <Button
        android:layout_width="wrap_content"
        android:text="加载"
        android:id="@+id/bt_loading"
        android:onClick="onClick"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="wrap_content"
        android:text="成功"
        android:onClick="onClick"
        android:id="@+id/bt_success"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="wrap_content"
        android:text="失败"
        android:onClick="onClick"
        android:id="@+id/bt_faild"
        android:layout_height="wrap_content" />
</LinearLayout>
<com.example.administrator.jianshupro.view.LoadingLayout
    android:id="@+id/myloadlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</com.example.administrator.jianshupro.view.LoadingLayout>

</LinearLayout>

6.在Activity中实现逻辑

public class MainActivity extends AppCompatActivity {
private LoadingLayout mLoadingLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mLoadingLayout=findViewById(R.id.myloadlayout);
    //添加状态View
    mLoadingLayout.setStatusView(View.inflate(this,R.layout.view_loading,null),View.inflate(this,R.layout.view_success,null),View.inflate(this,R.layout.view_faild,null));

}
public void onClick(View view){
    switch (view.getId()){
        case R.id.bt_loading:
            mLoadingLayout.setStatus(LoadingLayout.LoadingType.LOADING);
            break;
        case R.id.bt_success:
            mLoadingLayout.setStatus(LoadingLayout.LoadingType.SUCCESS);
            break;
        case R.id.bt_faild:
            mLoadingLayout.setStatus(LoadingLayout.LoadingType.FAILD);
            break;
    }
}
}
上一篇:动画


下一篇:批量清除nginx缓存的方法