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