Skip to content

Commit 928fc3e

Browse files
committed
update demo
1 parent d1fe663 commit 928fc3e

11 files changed

Lines changed: 144 additions & 51 deletions

File tree

multistatelayout/src/main/java/cn/refactor/multistatelayout/MultiStateLayout.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class MultiStateLayout extends FrameLayout {
5151
private int mLoadingResId;
5252
private int mNetworkErrorResId;
5353

54+
private LayoutInflater mInflater;
55+
5456
@IntDef({State.CONTENT, State.EMPTY, State.LOADING, State.ERROR, State.NETWORK_ERROR})
5557
@Retention(RetentionPolicy.SOURCE)
5658
public @interface State {
@@ -89,6 +91,8 @@ private void init(AttributeSet attrs) {
8991
mLoadingResId = ta.getResourceId(R.styleable.MultiStateLayout_loading_layout, getCommonLayoutResIdByState(State.LOADING));
9092
mNetworkErrorResId = ta.getResourceId(R.styleable.MultiStateLayout_network_error_layout, getCommonLayoutResIdByState(State.NETWORK_ERROR));
9193
ta.recycle();
94+
95+
mInflater = LayoutInflater.from(getContext());
9296
}
9397

9498
@Override
@@ -131,6 +135,7 @@ public void setState(@State int state) {
131135
*
132136
* @param resId layout
133137
*/
138+
@SuppressWarnings("unused")
134139
public void setEmptyView(@LayoutRes int resId) {
135140
if (null != mEmptyView) {
136141
removeView(mEmptyView);
@@ -144,6 +149,7 @@ public void setEmptyView(@LayoutRes int resId) {
144149
*
145150
* @param emptyView view
146151
*/
152+
@SuppressWarnings("unused")
147153
public void setEmptyView(View emptyView) {
148154
removeView(mEmptyView);
149155
mEmptyView = emptyView;
@@ -156,10 +162,11 @@ public void setEmptyView(View emptyView) {
156162
*
157163
* @return view
158164
*/
165+
@SuppressWarnings("unused")
159166
public View getEmptyView() {
160167
if (null == mEmptyView) {
161168
if (mEmptyResId > -1) {
162-
mEmptyView = LayoutInflater.from(getContext()).inflate(mEmptyResId, this, false);
169+
mEmptyView = mInflater.inflate(mEmptyResId, this, false);
163170
addView(mEmptyView, mEmptyView.getLayoutParams());
164171
mEmptyView.setVisibility(GONE);
165172
}
@@ -172,6 +179,7 @@ public View getEmptyView() {
172179
*
173180
* @param resId layout
174181
*/
182+
@SuppressWarnings("unused")
175183
public void setLoadingView(@LayoutRes int resId) {
176184
if (null != mLoadingView) {
177185
removeView(mLoadingView);
@@ -185,6 +193,7 @@ public void setLoadingView(@LayoutRes int resId) {
185193
*
186194
* @param loadingView view
187195
*/
196+
@SuppressWarnings("unused")
188197
public void setLoadingView(View loadingView) {
189198
removeView(mLoadingView);
190199
mLoadingView = loadingView;
@@ -197,10 +206,11 @@ public void setLoadingView(View loadingView) {
197206
*
198207
* @return view
199208
*/
209+
@SuppressWarnings("unused")
200210
public View getLoadingView() {
201211
if (null == mLoadingView) {
202212
if (mLoadingResId > -1) {
203-
mLoadingView = LayoutInflater.from(getContext()).inflate(mLoadingResId, this, false);
213+
mLoadingView = mInflater.inflate(mLoadingResId, this, false);
204214
addView(mLoadingView, mLoadingView.getLayoutParams());
205215
mLoadingView.setVisibility(GONE);
206216
}
@@ -213,6 +223,7 @@ public View getLoadingView() {
213223
*
214224
* @param resId layout
215225
*/
226+
@SuppressWarnings("unused")
216227
public void setErrorView(@LayoutRes int resId) {
217228
if (null != mErrorView) {
218229
removeView(mErrorView);
@@ -226,6 +237,7 @@ public void setErrorView(@LayoutRes int resId) {
226237
*
227238
* @param errorView view
228239
*/
240+
@SuppressWarnings("unused")
229241
public void setErrorView(View errorView) {
230242
removeView(mErrorView);
231243
mErrorView = errorView;
@@ -238,10 +250,11 @@ public void setErrorView(View errorView) {
238250
*
239251
* @return view
240252
*/
253+
@SuppressWarnings("unused")
241254
public View getErrorView() {
242255
if (null == mErrorView) {
243256
if (mErrorResId > -1) {
244-
mErrorView = LayoutInflater.from(getContext()).inflate(mErrorResId, this, false);
257+
mErrorView = mInflater.inflate(mErrorResId, this, false);
245258
addView(mErrorView, mErrorView.getLayoutParams());
246259
mErrorView.setVisibility(GONE);
247260
}
@@ -254,6 +267,7 @@ public View getErrorView() {
254267
*
255268
* @param resId layout
256269
*/
270+
@SuppressWarnings("unused")
257271
public void setNetworkErrorView(@LayoutRes int resId) {
258272
if (null != mNetworkErrorView) {
259273
removeView(mNetworkErrorView);
@@ -267,6 +281,7 @@ public void setNetworkErrorView(@LayoutRes int resId) {
267281
*
268282
* @param networkErrorView view
269283
*/
284+
@SuppressWarnings("unused")
270285
public void setNetworkErrorView(View networkErrorView) {
271286
removeView(mNetworkErrorView);
272287
mNetworkErrorView = networkErrorView;
@@ -279,10 +294,11 @@ public void setNetworkErrorView(View networkErrorView) {
279294
*
280295
* @return view
281296
*/
297+
@SuppressWarnings("unused")
282298
public View getNetworkErrorView() {
283299
if (null == mNetworkErrorView) {
284300
if (mNetworkErrorResId > -1) {
285-
mNetworkErrorView = LayoutInflater.from(getContext()).inflate(mNetworkErrorResId, this, false);
301+
mNetworkErrorView = mInflater.inflate(mNetworkErrorResId, this, false);
286302
addView(mNetworkErrorView, mNetworkErrorView.getLayoutParams());
287303
mNetworkErrorView.setVisibility(GONE);
288304
}
@@ -363,11 +379,13 @@ private void showContentView() {
363379
*/
364380
private void showEmptyView() {
365381
if (null == mEmptyView && mEmptyResId > -1) {
366-
mEmptyView = LayoutInflater.from(getContext()).inflate(mEmptyResId, this, false);
382+
mEmptyView = mInflater.inflate(mEmptyResId, this, false);
367383
addView(mEmptyView, mEmptyView.getLayoutParams());
368384
}
369385
if (null != mEmptyView) {
370386
mEmptyView.setVisibility(VISIBLE);
387+
} else {
388+
throw new NullPointerException("Expect to have an empty view.");
371389
}
372390
}
373391

@@ -376,11 +394,13 @@ private void showEmptyView() {
376394
*/
377395
private void showLoadingView() {
378396
if (null == mLoadingView && mLoadingResId > -1) {
379-
mLoadingView = LayoutInflater.from(getContext()).inflate(mLoadingResId, this, false);
397+
mLoadingView = mInflater.inflate(mLoadingResId, this, false);
380398
addView(mLoadingView, mLoadingView.getLayoutParams());
381399
}
382400
if (null != mLoadingView) {
383401
mLoadingView.setVisibility(VISIBLE);
402+
} else {
403+
throw new NullPointerException("Expect to have an loading view.");
384404
}
385405
}
386406

@@ -389,11 +409,13 @@ private void showLoadingView() {
389409
*/
390410
private void showErrorView() {
391411
if (null == mErrorView && mErrorResId > -1) {
392-
mErrorView = LayoutInflater.from(getContext()).inflate(mErrorResId, this, false);
412+
mErrorView = mInflater.inflate(mErrorResId, this, false);
393413
addView(mErrorView, mErrorView.getLayoutParams());
394414
}
395415
if (null != mErrorView) {
396416
mErrorView.setVisibility(VISIBLE);
417+
} else {
418+
throw new NullPointerException("Expect to have one error view.");
397419
}
398420
}
399421

@@ -402,11 +424,13 @@ private void showErrorView() {
402424
*/
403425
private void showNetworkErrorView() {
404426
if (null == mNetworkErrorView && mNetworkErrorResId > -1) {
405-
mNetworkErrorView = LayoutInflater.from(getContext()).inflate(mNetworkErrorResId, this, false);
427+
mNetworkErrorView = mInflater.inflate(mNetworkErrorResId, this, false);
406428
addView(mNetworkErrorView, mNetworkErrorView.getLayoutParams());
407429
}
408430
if (null != mNetworkErrorView) {
409431
mNetworkErrorView.setVisibility(VISIBLE);
432+
} else {
433+
throw new NullPointerException("Expect to have one network error view.");
410434
}
411435
}
412436

sample/src/main/java/cn/refactor/multistatelayoutdemo/SecondActivity.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import android.support.annotation.Nullable;
55
import android.support.v7.app.AppCompatActivity;
66

7-
import cn.refactor.multistatelayout.MultiStateLayout;
8-
97

108
/**
119
* Created by andy (https://github.com/andyxialm)
@@ -18,11 +16,10 @@ public class SecondActivity extends AppCompatActivity {
1816
protected void onCreate(@Nullable Bundle savedInstanceState) {
1917
super.onCreate(savedInstanceState);
2018
setContentView(R.layout.activity_second);
21-
setupViews();
22-
}
2319

24-
private void setupViews() {
25-
MultiStateLayout multiStateLayout = (MultiStateLayout) findViewById(R.id.multi_state_layout);
26-
multiStateLayout.setState(MultiStateLayout.State.EMPTY);
20+
getFragmentManager()
21+
.beginTransaction()
22+
.replace(R.id.fragment_container, SecondFragment.newInstance())
23+
.commit();
2724
}
2825
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package cn.refactor.multistatelayoutdemo;
2+
3+
import android.app.Fragment;
4+
import android.os.Bundle;
5+
import android.support.annotation.Nullable;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
import android.widget.TextView;
10+
11+
import cn.refactor.multistatelayout.MultiStateLayout;
12+
13+
14+
/**
15+
* Created by andy (https://github.com/andyxialm)
16+
* Creation time: 17/1/10 14:34
17+
* Description: Second fragment
18+
*/
19+
public class SecondFragment extends Fragment {
20+
21+
private MultiStateLayout mMultiStateLayout;
22+
23+
public static SecondFragment newInstance() {
24+
return new SecondFragment();
25+
}
26+
27+
@Nullable
28+
@Override
29+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
30+
return inflater.inflate(R.layout.fragment_second, container, false);
31+
}
32+
33+
@Override
34+
public void onViewCreated(View view, Bundle savedInstanceState) {
35+
super.onViewCreated(view, savedInstanceState);
36+
mMultiStateLayout = (MultiStateLayout) view.findViewById(R.id.multi_state_layout);
37+
mMultiStateLayout.setState(MultiStateLayout.State.LOADING);
38+
39+
mMultiStateLayout.postDelayed(new Runnable() {
40+
@Override
41+
public void run() {
42+
mMultiStateLayout.setState(MultiStateLayout.State.NETWORK_ERROR);
43+
}
44+
}, 2000);
45+
46+
View networkErrorView = mMultiStateLayout.getNetworkErrorView();
47+
if (null != networkErrorView) {
48+
((TextView) networkErrorView.findViewById(R.id.tv_msg)).setText(R.string.mock_text);
49+
networkErrorView.findViewById(R.id.btn_reload).setOnClickListener(new View.OnClickListener() {
50+
@Override
51+
public void onClick(View view) {
52+
mMultiStateLayout.setState(MultiStateLayout.State.CONTENT);
53+
}
54+
});
55+
}
56+
}
57+
}
Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout
2+
<FrameLayout
3+
android:id="@+id/fragment_container"
34
xmlns:android="http://schemas.android.com/apk/res/android"
45
android:layout_width="match_parent"
5-
android:layout_height="match_parent"
6-
android:orientation="vertical">
6+
android:layout_height="match_parent">
77

8-
<android.support.v7.widget.Toolbar
9-
android:id="@+id/toolbar"
10-
xmlns:app="http://schemas.android.com/apk/res-auto"
11-
android:layout_width="match_parent"
12-
android:layout_height="?attr/actionBarSize"
13-
android:background="?attr/colorPrimary"
14-
app:title="@string/title_second"
15-
app:titleTextColor="@android:color/white"/>
16-
17-
<cn.refactor.multistatelayout.MultiStateLayout
18-
android:id="@+id/multi_state_layout"
19-
xmlns:android="http://schemas.android.com/apk/res/android"
20-
xmlns:state="http://schemas.android.com/apk/res-auto"
21-
android:layout_width="match_parent"
22-
android:layout_height="match_parent"
23-
state:network_error_layout="@layout/layout_custom_network_error">
24-
25-
<TextView
26-
android:layout_width="wrap_content"
27-
android:layout_height="wrap_content"
28-
android:layout_gravity="center"
29-
android:text="Hello World!"/>
30-
31-
</cn.refactor.multistatelayout.MultiStateLayout>
32-
33-
</LinearLayout>
8+
</FrameLayout>
349

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent">
6+
7+
<android.support.v7.widget.Toolbar
8+
android:id="@+id/toolbar"
9+
xmlns:app="http://schemas.android.com/apk/res-auto"
10+
android:layout_width="match_parent"
11+
android:layout_height="?attr/actionBarSize"
12+
android:background="?attr/colorPrimary"
13+
app:title="@string/title_second"
14+
app:titleTextColor="@android:color/white"/>
15+
16+
<cn.refactor.multistatelayout.MultiStateLayout
17+
android:id="@+id/multi_state_layout"
18+
xmlns:android="http://schemas.android.com/apk/res/android"
19+
xmlns:state="http://schemas.android.com/apk/res-auto"
20+
android:layout_width="match_parent"
21+
android:layout_height="match_parent"
22+
state:network_error_layout="@layout/layout_custom_network_error">
23+
24+
<TextView
25+
android:layout_width="wrap_content"
26+
android:layout_height="wrap_content"
27+
android:layout_gravity="center"
28+
android:text="Hello World!"/>
29+
30+
</cn.refactor.multistatelayout.MultiStateLayout>
31+
32+
</FrameLayout>
33+

sample/src/main/res/layout/layout_custom_network_error.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
android:layout_width="wrap_content"
1414
android:layout_height="wrap_content"
1515
android:layout_centerInParent="true"
16-
android:text="Network Error"/>
16+
android:text="@string/network_error"/>
1717

1818
<Button
1919
android:id="@+id/btn_reload"
@@ -22,6 +22,6 @@
2222
android:layout_centerInParent="true"
2323
android:layout_above="@id/tv_msg"
2424
android:layout_marginBottom="@dimen/activity_vertical_margin"
25-
android:text="Reload"/>
25+
android:text="@string/reload"/>
2626

2727
</RelativeLayout>

sample/src/main/res/layout/layout_empty.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
android:layout_width="wrap_content"
55
android:layout_height="wrap_content"
66
android:layout_gravity="center"
7-
android:text="Empty"/>
7+
android:text="@string/empty"/>
88

sample/src/main/res/layout/layout_error.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
android:layout_width="wrap_content"
55
android:layout_height="wrap_content"
66
android:layout_gravity="center"
7-
android:text="Error"/>
7+
android:text="@string/error"/>

sample/src/main/res/layout/layout_loading.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<android.support.v4.widget.ContentLoadingProgressBar
2+
<ProgressBar
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
style="?android:attr/progressBarStyle"
55
android:layout_width="wrap_content"

sample/src/main/res/layout/layout_network_error.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
android:layout_width="wrap_content"
55
android:layout_height="wrap_content"
66
android:layout_gravity="center"
7-
android:text="Network Error"/>
7+
android:text="@string/network_error"/>

0 commit comments

Comments
 (0)