android 界面状态封装,可快速切换,一个view就可以搞定!

Statelayout 界面状态快速切换

在app使用中会经常切换界面状态,比如,加载中,加载失败,网络错误等等,所以封装了一个工具类。使用起来也非常的简单, 只要在布局文件中声明,并设置各种状态图标,文字等。
效果:

使用:

在layout中:
<com.sushanqiang.statelayout.StateLayout
        xmlns:sl="http://schemas.android.com/apk/res-auto"
        android:id="@+id/state_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/ll_button1"
        sl:emptyImg="@drawable/ic_state_empty"
        sl:emptyText="@string/empty_tip"
        sl:errorImg="@drawable/ic_state_error"
        sl:errorText="@string/error_tip"
        sl:loadingText="@string/loading_tip"
        sl:loginImg="@drawable/ic_state_login"
        sl:loginText="@string/no_login_tip"
        sl:noNetworkImg="@drawable/ic_state_no_network"
        sl:noNetworkText="@string/no_network_tip"
        sl:timeOutImg="@drawable/ic_state_time_out"
        sl:timeOutText="@string/time_out_tip">
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    StateLayout stateLayout;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        stateLayout = (StateLayout) this.findViewById(R.id.state_layout);
        stateLayout.setUseAnimation(true);
        stateLayout.showTimeoutView();
//        stateLayout.setViewSwitchAnimProvider(new FadeScaleViewAnimProvider());
        stateLayout.setRefreshListener(new StateLayout.OnViewRefreshListener() {
            @Override
            public void refreshClick() {
                stateLayout.showLoadingView();
                Toast.makeText( MainActivity.this, "刷新", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void loginClick() {
                Toast.makeText(MainActivity.this, "登录", Toast.LENGTH_SHORT).show();
            }
        });

        findViewById(R.id.btn_content).setOnClickListener(this);
        findViewById(R.id.btn_empty).setOnClickListener(this);
        findViewById(R.id.btn_error).setOnClickListener(this);
        findViewById(R.id.btn_loading).setOnClickListener(this);
        findViewById(R.id.btn_time_out).setOnClickListener(this);
        findViewById(R.id.btn_not_network).setOnClickListener(this);
        findViewById(R.id.btn_login).setOnClickListener(this);
        findViewById(R.id.btn_custom).setOnClickListener(this);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_content:
                stateLayout.showContentView();
//                stateLayout.setTipText(StateLayout.EMPTY, "12345");
//                stateLayout.setTipImg(StateLayout.EMPTY, R.mipmap.ic_launcher);
                break;
            case R.id.btn_empty:
                stateLayout.showEmptyView();
//                stateLayout.setTipText(StateLayout.ERROR, "12345");
//                stateLayout.setTipImg(StateLayout.ERROR, R.mipmap.ic_launcher);
                break;
            case R.id.btn_error:
                stateLayout.showErrorView();
                stateLayout.setTipText(StateLayout.ERROR,"请稍后再试哦");
                stateLayout.setTipImg(StateLayout.ERROR, R.mipmap.ic_launcher);
//                stateLayout.setTipText(StateLayout.LOADING, "12345");
                break;
            case R.id.btn_loading:
                stateLayout.showLoadingView();
//                stateLayout.setTipText(StateLayout.TIMEOUT, "12345");
//                stateLayout.setTipImg(StateLayout.TIMEOUT, R.mipmap.ic_launcher);
                break;
            case R.id.btn_time_out:
                stateLayout.showTimeoutView();
//                stateLayout.setTipText(StateLayout.NOT_NETWORK, "12345");
//                stateLayout.setTipImg(StateLayout.NOT_NETWORK, R.mipmap.ic_launcher);
                break;
            case R.id.btn_not_network:
                stateLayout.showNoNetworkView();
                break;
            case R.id.btn_login:
                stateLayout.showLoginView();
                break;

        }
    }
}

如何添加

Gradle

1.在Project的build.gradle 中添加仓库地址
 // JitPack仓库地址
 maven { url "https://jitpack.io" }

示例:

allprojects {
    repositories {
        jcenter()
        // JitPack仓库地址
        maven { url "https://jitpack.io" }
    }
}
2.在Module目录下的build.gradle中添加依赖
  //添加依赖
    compile 'com.github.sushanqiang:StatelayoutLibrary:v1.0.0'

示例:

在主项目module的 build.gradle中添加:
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:25.1.0'
    //添加依赖
    compile 'com.github.sushanqiang:StatelayoutLibrary:v1.0.0'
}

Github 地址及demo:

https://github.com/sushanqiang/StatelayoutLibrary

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值