Skip to content

Commit a96a57f

Browse files
committed
Add support for empty state (ContentState.EMPTY).
* Useful in cases where the content depends on user actions: when there is no content to show, an empty state notifying the user of the same looks better than a completely blank view. * The library doesn't play well with AbsListView#setEmptyView(), so this is a good alternative.
1 parent 9bd1461 commit a96a57f

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

Diff for: res/layout/msv__empty.xml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent">
6+
7+
<TextView
8+
android:id="@+id/empty"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
android:layout_gravity="center"
12+
android:text="@string/nothing_to_show"/>
13+
14+
</FrameLayout>

Diff for: res/values/msv__attrs.xml

+3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
<enum name="error_network" value="0x02" />
1313
<!-- The state is to show loading indication -->
1414
<enum name="error_unknown" value="0x03" />
15+
<!-- The state is to show empty view state -->
16+
<enum name="empty" value="0x04" />
1517
</attr>
1618

19+
<attr name="msvEmptyLayout" format="reference" />
1720
<attr name="msvLoadingLayout" format="reference" />
1821
<attr name="msvErrorUnknownLayout" format="reference" />
1922
<attr name="msvErrorNetworkLayout" format="reference" />

Diff for: res/values/msv__strings.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
<string name="error_title_network">Network Error</string>
55
<string name="error_title_unknown">Oops! We messed up.</string>
66
<string name="tap_to_retry">Tap to retry</string>
7+
<string name="nothing_to_show">Nothing to show</string>
78

8-
</resources>
9+
</resources>

Diff for: src/com/meetme/android/multistateview/MultiStateView.java

+55-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class MultiStateView extends FrameLayout {
2121
private MultiStateViewData mViewState = new MultiStateViewData(ContentState.CONTENT);
2222

2323
private View mContentView;
24+
private View mEmptyView;
2425
private View mLoadingView;
2526
private View mNetworkErrorView;
2627
private View mGeneralErrorView;
@@ -55,6 +56,7 @@ private void parseAttrs(Context context, AttributeSet attrs) {
5556
setLoadingLayoutResourceId(a.getResourceId(R.styleable.MultiStateView_msvLoadingLayout, R.layout.msv__loading));
5657
setGeneralErrorLayoutResourceId(a.getResourceId(R.styleable.MultiStateView_msvErrorUnknownLayout, R.layout.msv__error_unknown));
5758
setNetworkErrorLayoutResourceId(a.getResourceId(R.styleable.MultiStateView_msvErrorNetworkLayout, R.layout.msv__error_network));
59+
setEmptyLayoutResourceId(a.getResourceId(R.styleable.MultiStateView_msvEmptyLayout, R.layout.msv__empty));
5860

5961
String tmpString;
6062

@@ -82,6 +84,14 @@ private void parseAttrs(Context context, AttributeSet attrs) {
8284

8385
setTapToRetryString(tmpString);
8486

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+
8595
setState(a.getInt(R.styleable.MultiStateView_msvState, ContentState.CONTENT.nativeInt));
8696
} finally {
8797
a.recycle();
@@ -140,6 +150,18 @@ public void setLoadingLayoutResourceId(int loadingLayout) {
140150
this.mViewState.loadingLayoutResId = loadingLayout;
141151
}
142152

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+
143165
/**
144166
* @return the {@link ContentState} the view is currently in
145167
*/
@@ -207,6 +229,9 @@ public void setState(final ContentState state) {
207229
*/
208230
public View getStateView(ContentState state) {
209231
switch (state) {
232+
case EMPTY:
233+
return getEmptyView();
234+
210235
case ERROR_NETWORK:
211236
return getNetworkErrorView();
212237

@@ -223,6 +248,21 @@ public View getStateView(ContentState state) {
223248
return null;
224249
}
225250

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+
226266
/**
227267
* Returns the view to be displayed for the case of a network error
228268
*
@@ -322,7 +362,8 @@ public void setContentView(View contentView) {
322362
}
323363

324364
private boolean isViewInternal(View view) {
325-
return view == mNetworkErrorView || view == mGeneralErrorView || view == mLoadingView;
365+
return view == mNetworkErrorView || view == mGeneralErrorView
366+
|| view == mLoadingView || view == mEmptyView;
326367
}
327368

328369
@Override
@@ -359,6 +400,8 @@ private void setViewState(MultiStateViewData state) {
359400
setNetworkErrorLayoutResourceId(state.networkErrorLayoutResId);
360401
setLoadingLayoutResourceId(state.loadingLayoutResId);
361402
setCustomErrorString(state.customErrorString);
403+
setEmptyLayoutResourceId(state.emptyLayoutResId);
404+
setEmptyString(state.emptyString);
362405
}
363406

364407
@Override
@@ -448,7 +491,13 @@ public static enum ContentState {
448491
*
449492
* @see R.attr#msvState
450493
*/
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);
452501

453502
public final int nativeInt;
454503
private final static SparseArray<ContentState> sStates = new SparseArray<ContentState>();
@@ -506,9 +555,11 @@ public static class MultiStateViewData implements Parcelable {
506555
public int loadingLayoutResId;
507556
public int generalErrorLayoutResId;
508557
public int networkErrorLayoutResId;
558+
public int emptyLayoutResId;
509559
public String networkErrorTitleString;
510560
public String generalErrorTitleString;
511561
public String tapToRetryString;
562+
public String emptyString;
512563
public ContentState state;
513564

514565
public MultiStateViewData(ContentState contentState) {
@@ -523,6 +574,7 @@ private MultiStateViewData(Parcel in) {
523574
networkErrorTitleString = in.readString();
524575
generalErrorTitleString = in.readString();
525576
tapToRetryString = in.readString();
577+
emptyString = in.readString();
526578
state = ContentState.valueOf(in.readString());
527579
}
528580

@@ -538,6 +590,7 @@ public void writeToParcel(Parcel dest, int flags) {
538590
dest.writeString(networkErrorTitleString);
539591
dest.writeString(generalErrorTitleString);
540592
dest.writeString(tapToRetryString);
593+
dest.writeString(emptyString);
541594
dest.writeString(state.name());
542595
}
543596

0 commit comments

Comments
 (0)