在app使用中會(huì)經(jīng)常切換界面狀態(tài)占卧,比如遗菠,加載中,加載失敗华蜒,網(wǎng)絡(luò)錯(cuò)誤等等辙纬,所以封裝了一個(gè)工具類。使用起來(lái)也非常的簡(jiǎn)單叭喜, 只要在布局文件中聲明贺拣,并設(shè)置各種狀態(tài)圖標(biāo),文字等捂蕴。
效果:
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,"請(qǐng)稍后再試哦");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 中添加倉(cāng)庫(kù)地址
// JitPack倉(cāng)庫(kù)地址maven { url"https://jitpack.io"}
示例:
allprojects {? ? repositories {? ? ? ? jcenter()// JitPack倉(cāng)庫(kù)地址maven { url"https://jitpack.io"}? ? }}
2.在Module目錄下的build.gradle中添加依賴
//添加依賴compile'com.github.sushanqiang:StatelayoutLibrary:v1.0.0'
在主項(xiàng)目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'}