2
2
3
3
import android .view .View ;
4
4
import android .view .ViewGroup ;
5
- import android .widget .FrameLayout ;
6
- import android .widget .LinearLayout ;
7
5
8
6
import com .codeest .geeknews .R ;
9
7
import com .codeest .geeknews .widget .ProgressImageView ;
@@ -21,11 +19,13 @@ public abstract class RootFragment<T extends BasePresenter> extends BaseFragment
21
19
private static final int STATE_ERROR = 0x02 ;
22
20
23
21
private ProgressImageView ivLoading ;
24
- private LinearLayout viewError ;
25
- private FrameLayout viewLoading ;
22
+ private View viewError ;
23
+ private View viewLoading ;
26
24
private ViewGroup viewMain ;
27
25
private ViewGroup mParent ;
28
26
27
+ private int mErrorResource = R .layout .view_error ;
28
+
29
29
private int currentState = STATE_MAIN ;
30
30
private boolean isErrorViewAdded = false ;
31
31
@@ -36,15 +36,15 @@ protected void initEventAndData() {
36
36
viewMain = (ViewGroup ) getView ().findViewById (R .id .view_main );
37
37
if (viewMain == null ) {
38
38
throw new IllegalStateException (
39
- "The subclass of RootActivity must contain a View named view_main." );
39
+ "The subclass of RootActivity must contain a View named ' view_main' ." );
40
40
}
41
41
if (!(viewMain .getParent () instanceof ViewGroup )) {
42
42
throw new IllegalStateException (
43
43
"view_main's ParentView should be a ViewGroup." );
44
44
}
45
45
mParent = (ViewGroup ) viewMain .getParent ();
46
46
View .inflate (mContext , R .layout .view_progress , mParent );
47
- viewLoading = ( FrameLayout ) mParent .findViewById (R .id .view_loading );
47
+ viewLoading = mParent .findViewById (R .id .view_loading );
48
48
ivLoading = (ProgressImageView ) viewLoading .findViewById (R .id .iv_progress );
49
49
viewLoading .setVisibility (View .GONE );
50
50
viewMain .setVisibility (View .VISIBLE );
@@ -56,8 +56,12 @@ public void stateError() {
56
56
return ;
57
57
if (!isErrorViewAdded ) {
58
58
isErrorViewAdded = true ;
59
- View .inflate (mContext , R .layout .view_error , mParent );
60
- viewError = (LinearLayout ) mParent .findViewById (R .id .view_error );
59
+ View .inflate (mContext , mErrorResource , mParent );
60
+ viewError = mParent .findViewById (R .id .view_error );
61
+ if (viewError == null ) {
62
+ throw new IllegalStateException (
63
+ "A View should be named 'view_error' in ErrorLayoutResource." );
64
+ }
61
65
}
62
66
hideCurrentView ();
63
67
currentState = STATE_ERROR ;
@@ -99,4 +103,8 @@ private void hideCurrentView() {
99
103
break ;
100
104
}
101
105
}
106
+
107
+ public void setErrorResource (int errorLayoutResource ) {
108
+ this .mErrorResource = errorLayoutResource ;
109
+ }
102
110
}
0 commit comments