1
+ package com .siberiadante .androidutil .view .pageview ;
2
+
3
+ import android .content .Context ;
4
+ import android .os .Looper ;
5
+ import android .util .AttributeSet ;
6
+ import android .view .LayoutInflater ;
7
+ import android .view .View ;
8
+ import android .widget .FrameLayout ;
9
+
10
+ import com .siberiadante .androidutil .util .SDLogUtil ;
11
+
12
+
13
+ public class LoadingAndRetryLayout extends FrameLayout {
14
+ private View mLoadingView ;
15
+ private View mRetryView ;
16
+ private View mContentView ;
17
+ private View mEmptyView ;
18
+ private LayoutInflater mInflater ;
19
+
20
+ private static final String TAG = LoadingAndRetryLayout .class .getSimpleName ();
21
+
22
+
23
+ public LoadingAndRetryLayout (Context context , AttributeSet attrs , int defStyleAttr ) {
24
+ super (context , attrs , defStyleAttr );
25
+ mInflater = LayoutInflater .from (context );
26
+ }
27
+
28
+
29
+ public LoadingAndRetryLayout (Context context , AttributeSet attrs ) {
30
+ this (context , attrs , -1 );
31
+ }
32
+
33
+ public LoadingAndRetryLayout (Context context ) {
34
+ this (context , null );
35
+ }
36
+
37
+ private boolean isMainThread () {
38
+ return Looper .myLooper () == Looper .getMainLooper ();
39
+ }
40
+
41
+ public void showLoading () {
42
+ if (isMainThread ()) {
43
+ showView (mLoadingView );
44
+ } else {
45
+ post (new Runnable () {
46
+ @ Override
47
+ public void run () {
48
+ showView (mLoadingView );
49
+ }
50
+ });
51
+ }
52
+ }
53
+
54
+ public void showRetry () {
55
+ if (isMainThread ()) {
56
+ showView (mRetryView );
57
+ } else {
58
+ post (new Runnable () {
59
+ @ Override
60
+ public void run () {
61
+ showView (mRetryView );
62
+ }
63
+ });
64
+ }
65
+
66
+ }
67
+
68
+ public void showContent () {
69
+ if (isMainThread ()) {
70
+ showView (mContentView );
71
+ } else {
72
+ post (new Runnable () {
73
+ @ Override
74
+ public void run () {
75
+ showView (mContentView );
76
+ }
77
+ });
78
+ }
79
+ }
80
+
81
+ public void showEmpty () {
82
+ if (isMainThread ()) {
83
+ showView (mEmptyView );
84
+ } else {
85
+ post (new Runnable () {
86
+ @ Override
87
+ public void run () {
88
+ showView (mEmptyView );
89
+ }
90
+ });
91
+ }
92
+ }
93
+
94
+
95
+ private void showView (View view ) {
96
+ if (view == null ) return ;
97
+
98
+ if (view == mLoadingView ) {
99
+ mLoadingView .setVisibility (View .VISIBLE );
100
+ if (mRetryView != null )
101
+ mRetryView .setVisibility (View .GONE );
102
+ if (mContentView != null )
103
+ mContentView .setVisibility (View .GONE );
104
+ if (mEmptyView != null )
105
+ mEmptyView .setVisibility (View .GONE );
106
+ } else if (view == mRetryView ) {
107
+ mRetryView .setVisibility (View .VISIBLE );
108
+ if (mLoadingView != null )
109
+ mLoadingView .setVisibility (View .GONE );
110
+ if (mContentView != null )
111
+ mContentView .setVisibility (View .GONE );
112
+ if (mEmptyView != null )
113
+ mEmptyView .setVisibility (View .GONE );
114
+ } else if (view == mContentView ) {
115
+ mContentView .setVisibility (View .VISIBLE );
116
+ if (mLoadingView != null )
117
+ mLoadingView .setVisibility (View .GONE );
118
+ if (mRetryView != null )
119
+ mRetryView .setVisibility (View .GONE );
120
+ if (mEmptyView != null )
121
+ mEmptyView .setVisibility (View .GONE );
122
+ } else if (view == mEmptyView ) {
123
+ mEmptyView .setVisibility (View .VISIBLE );
124
+ if (mLoadingView != null )
125
+ mLoadingView .setVisibility (View .GONE );
126
+ if (mRetryView != null )
127
+ mRetryView .setVisibility (View .GONE );
128
+ if (mContentView != null )
129
+ mContentView .setVisibility (View .GONE );
130
+ }
131
+
132
+
133
+ }
134
+
135
+ public View setContentView (int layoutId ) {
136
+ return setContentView (mInflater .inflate (layoutId , this , false ));
137
+ }
138
+
139
+ public View setLoadingView (int layoutId ) {
140
+ return setLoadingView (mInflater .inflate (layoutId , this , false ));
141
+ }
142
+
143
+ public View setEmptyView (int layoutId ) {
144
+ return setEmptyView (mInflater .inflate (layoutId , this , false ));
145
+ }
146
+
147
+ public View setRetryView (int layoutId ) {
148
+ return setRetryView (mInflater .inflate (layoutId , this , false ));
149
+ }
150
+
151
+ public View setLoadingView (View view ) {
152
+ View loadingView = mLoadingView ;
153
+ if (loadingView != null ) {
154
+ SDLogUtil .w (TAG , "you have already set a loading view and would be instead of this new one." );
155
+ }
156
+ removeView (loadingView );
157
+ addView (view );
158
+ mLoadingView = view ;
159
+ return mLoadingView ;
160
+ }
161
+
162
+ public View setEmptyView (View view ) {
163
+ View emptyView = mEmptyView ;
164
+ if (emptyView != null ) {
165
+ SDLogUtil .w (TAG , "you have already set a empty view and would be instead of this new one." );
166
+ }
167
+ removeView (emptyView );
168
+ addView (view );
169
+ mEmptyView = view ;
170
+ return mEmptyView ;
171
+ }
172
+
173
+ public View setRetryView (View view ) {
174
+ View retryView = mRetryView ;
175
+ if (retryView != null ) {
176
+ SDLogUtil .w (TAG , "you have already set a retry view and would be instead of this new one." );
177
+ }
178
+ removeView (retryView );
179
+ addView (view );
180
+ mRetryView = view ;
181
+ return mRetryView ;
182
+
183
+ }
184
+
185
+ public View setContentView (View view ) {
186
+ View contentView = mContentView ;
187
+ if (contentView != null ) {
188
+ SDLogUtil .w (TAG , "you have already set a retry view and would be instead of this new one." );
189
+ }
190
+ removeView (contentView );
191
+ addView (view );
192
+ mContentView = view ;
193
+ return mContentView ;
194
+ }
195
+
196
+ public View getRetryView () {
197
+ return mRetryView ;
198
+ }
199
+
200
+ public View getLoadingView () {
201
+ return mLoadingView ;
202
+ }
203
+
204
+ public View getContentView () {
205
+ return mContentView ;
206
+ }
207
+
208
+ public View getEmptyView () {
209
+ return mEmptyView ;
210
+ }
211
+ }
0 commit comments