@@ -21,6 +21,7 @@ public class MultiStateView extends FrameLayout {
21
21
private MultiStateViewData mViewState = new MultiStateViewData (ContentState .CONTENT );
22
22
23
23
private View mContentView ;
24
+ private View mEmptyView ;
24
25
private View mLoadingView ;
25
26
private View mNetworkErrorView ;
26
27
private View mGeneralErrorView ;
@@ -55,6 +56,7 @@ private void parseAttrs(Context context, AttributeSet attrs) {
55
56
setLoadingLayoutResourceId (a .getResourceId (R .styleable .MultiStateView_msvLoadingLayout , R .layout .msv__loading ));
56
57
setGeneralErrorLayoutResourceId (a .getResourceId (R .styleable .MultiStateView_msvErrorUnknownLayout , R .layout .msv__error_unknown ));
57
58
setNetworkErrorLayoutResourceId (a .getResourceId (R .styleable .MultiStateView_msvErrorNetworkLayout , R .layout .msv__error_network ));
59
+ setEmptyLayoutResourceId (a .getResourceId (R .styleable .MultiStateView_msvEmptyLayout , R .layout .msv__empty ));
58
60
59
61
String tmpString ;
60
62
@@ -82,6 +84,14 @@ private void parseAttrs(Context context, AttributeSet attrs) {
82
84
83
85
setTapToRetryString (tmpString );
84
86
87
+ tmpString = a .getString (R .styleable .MultiStateView_msvEmptyLayout );
88
+
89
+ if (tmpString == null ) {
90
+ tmpString = context .getString (R .string .nothing_to_show );
91
+ }
92
+
93
+ setEmptyString (tmpString );
94
+
85
95
setState (a .getInt (R .styleable .MultiStateView_msvState , ContentState .CONTENT .nativeInt ));
86
96
} finally {
87
97
a .recycle ();
@@ -140,6 +150,18 @@ public void setLoadingLayoutResourceId(int loadingLayout) {
140
150
this .mViewState .loadingLayoutResId = loadingLayout ;
141
151
}
142
152
153
+ private void setEmptyLayoutResourceId (int resourceId ) {
154
+ mViewState .emptyLayoutResId = resourceId ;
155
+ }
156
+
157
+ private void setEmptyString (String string ) {
158
+ mViewState .emptyString = string ;
159
+ }
160
+
161
+ public String getEmptyString () {
162
+ return mViewState .emptyString ;
163
+ }
164
+
143
165
/**
144
166
* @return the {@link ContentState} the view is currently in
145
167
*/
@@ -207,6 +229,9 @@ public void setState(final ContentState state) {
207
229
*/
208
230
public View getStateView (ContentState state ) {
209
231
switch (state ) {
232
+ case EMPTY :
233
+ return getEmptyView ();
234
+
210
235
case ERROR_NETWORK :
211
236
return getNetworkErrorView ();
212
237
@@ -223,6 +248,21 @@ public View getStateView(ContentState state) {
223
248
return null ;
224
249
}
225
250
251
+ /**
252
+ * Returns the view to be displayed when there is nothing to show (e.g., no search results)
253
+ */
254
+ public View getEmptyView () {
255
+ if (mEmptyView == null ) {
256
+ mEmptyView = View .inflate (getContext (), mViewState .emptyLayoutResId , null );
257
+
258
+ ((TextView ) mEmptyView .findViewById (R .id .empty )).setText (getEmptyString ());
259
+
260
+ addView (mEmptyView );
261
+ }
262
+
263
+ return mEmptyView ;
264
+ }
265
+
226
266
/**
227
267
* Returns the view to be displayed for the case of a network error
228
268
*
@@ -322,7 +362,8 @@ public void setContentView(View contentView) {
322
362
}
323
363
324
364
private boolean isViewInternal (View view ) {
325
- return view == mNetworkErrorView || view == mGeneralErrorView || view == mLoadingView ;
365
+ return view == mNetworkErrorView || view == mGeneralErrorView
366
+ || view == mLoadingView || view == mEmptyView ;
326
367
}
327
368
328
369
@ Override
@@ -359,6 +400,8 @@ private void setViewState(MultiStateViewData state) {
359
400
setNetworkErrorLayoutResourceId (state .networkErrorLayoutResId );
360
401
setLoadingLayoutResourceId (state .loadingLayoutResId );
361
402
setCustomErrorString (state .customErrorString );
403
+ setEmptyLayoutResourceId (state .emptyLayoutResId );
404
+ setEmptyString (state .emptyString );
362
405
}
363
406
364
407
@ Override
@@ -448,7 +491,13 @@ public static enum ContentState {
448
491
*
449
492
* @see R.attr#msvState
450
493
*/
451
- ERROR_GENERAL (0x03 );
494
+ ERROR_GENERAL (0x03 ),
495
+ /**
496
+ * Used to indicate that the Empty indication should be displayed to the user
497
+ *
498
+ * @see R.attr#msvState
499
+ */
500
+ EMPTY (0x04 );
452
501
453
502
public final int nativeInt ;
454
503
private final static SparseArray <ContentState > sStates = new SparseArray <ContentState >();
@@ -506,9 +555,11 @@ public static class MultiStateViewData implements Parcelable {
506
555
public int loadingLayoutResId ;
507
556
public int generalErrorLayoutResId ;
508
557
public int networkErrorLayoutResId ;
558
+ public int emptyLayoutResId ;
509
559
public String networkErrorTitleString ;
510
560
public String generalErrorTitleString ;
511
561
public String tapToRetryString ;
562
+ public String emptyString ;
512
563
public ContentState state ;
513
564
514
565
public MultiStateViewData (ContentState contentState ) {
@@ -523,6 +574,7 @@ private MultiStateViewData(Parcel in) {
523
574
networkErrorTitleString = in .readString ();
524
575
generalErrorTitleString = in .readString ();
525
576
tapToRetryString = in .readString ();
577
+ emptyString = in .readString ();
526
578
state = ContentState .valueOf (in .readString ());
527
579
}
528
580
@@ -538,6 +590,7 @@ public void writeToParcel(Parcel dest, int flags) {
538
590
dest .writeString (networkErrorTitleString );
539
591
dest .writeString (generalErrorTitleString );
540
592
dest .writeString (tapToRetryString );
593
+ dest .writeString (emptyString );
541
594
dest .writeString (state .name ());
542
595
}
543
596
0 commit comments