----- 效果圖
------- 添加依賴
compile 'com.helin.loadinglayout:loadinglayout:0.0.1'
------ layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3000"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_loading"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="loading" />
<Button
android:id="@+id/btn_empty"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="empty" />
<Button
android:id="@+id/btn_fail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="fail" />
<Button
android:id="@+id/btn_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="content" />
</LinearLayout>
<com.helin.loadinglayout.LoadingLayout
android:id="@+id/loading_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="2dp"></ListView>
</com.helin.loadinglayout.LoadingLayout>
</LinearLayout>
---- 核心代碼是
<com.helin.loadinglayout.LoadingLayout
android:id="@+id/loading_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="2dp"></ListView>
</com.helin.loadinglayout.LoadingLayout>
------ code
private ListView mListView;
private LoadingLayout loadingLayout;
private Button btnLoading;
private Button btnEmpty;
private Button btnFail;
private Button btnContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_statelayout);
initView();
initData();
initListenter();
}
private void initView() {
mListView = (ListView) findViewById(R.id.list_view);
loadingLayout = (LoadingLayout) findViewById(R.id.loading_layout);
/*btn*/
btnLoading = (Button) findViewById(R.id.btn_loading);
btnEmpty = (Button) findViewById(R.id.btn_empty);
btnFail = (Button) findViewById(R.id.btn_fail);
btnContent = (Button) findViewById(R.id.btn_content);
}
private void initData() {
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
setStrings(stringArrayAdapter);
mListView.setAdapter(stringArrayAdapter);
}
private void initListenter() {
btnLoading.setOnClickListener(this);
btnEmpty.setOnClickListener(this);
btnFail.setOnClickListener(this);
btnContent.setOnClickListener(this);
/*loadingLayout 爲fail設置點擊事件*/
loadingLayout.setViewOncClickListener(R.id.state_retry,
new View.OnClickListener() {
@Override
public void onClick(View v) {
loadingLayout.showLoading();
}
});
}
@NonNull
private void setStrings(ArrayAdapter<String> arrayAdapter) {
for (int i = 0; i < 30; i++) {
arrayAdapter.add("item" + i);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_loading:
loadingLayout.showLoading();
break;
case R.id.btn_empty:
loadingLayout.showEmpty();
break;
case R.id.btn_fail:
loadingLayout.showState();
break;
case R.id.btn_content:
loadingLayout.showContent();
break;
default:
break;
}
}
---- 定製material progressbar 樣式
------ stateLayout的思路
添加4種子佈局,通過控制佈局的顯示和隱藏達到目的。
---- 參考blog
簡書 _小河馬 點擊打開鏈接