Android手写状态切换布局

实现状态切换布局

img_2c0a8d63364bb2a3beb1082c64f97772.png
image

效果图

img_6b23090d49db14f866045ce2c3574632.gif
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;
    }
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值