您的位置:首页 > 移动开发 > Android开发

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

2017-04-20 17:08 429 查看

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  网络 布局 界面
相关文章推荐