Skip to content

Commit 4866c05

Browse files
committed
v1.1.1
1 parent 72bf645 commit 4866c05

File tree

3 files changed

+408
-0
lines changed

3 files changed

+408
-0
lines changed
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
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+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package com.siberiadante.androidutil.view.pageview;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
import android.support.v4.app.Fragment;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
9+
10+
public class LoadingAndRetryManager {
11+
public static final int NO_LAYOUT_ID = 0;
12+
public static int BASE_LOADING_LAYOUT_ID = NO_LAYOUT_ID;
13+
public static int BASE_RETRY_LAYOUT_ID = NO_LAYOUT_ID;
14+
public static int BASE_EMPTY_LAYOUT_ID = NO_LAYOUT_ID;
15+
16+
public LoadingAndRetryLayout mLoadingAndRetryLayout;
17+
18+
19+
public OnLoadingAndRetryListener DEFAULT_LISTENER = new OnLoadingAndRetryListener() {
20+
@Override
21+
public void setRetryEvent(View retryView) {
22+
23+
}
24+
};
25+
26+
27+
public LoadingAndRetryManager(Object activityOrFragmentOrView, OnLoadingAndRetryListener listener) {
28+
if (listener == null) listener = DEFAULT_LISTENER;
29+
30+
ViewGroup contentParent = null;
31+
Context context;
32+
if (activityOrFragmentOrView instanceof Activity) {
33+
Activity activity = (Activity) activityOrFragmentOrView;
34+
context = activity;
35+
contentParent = (ViewGroup) activity.findViewById(android.R.id.content);
36+
} else if (activityOrFragmentOrView instanceof Fragment) {
37+
Fragment fragment = (Fragment) activityOrFragmentOrView;
38+
context = fragment.getActivity();
39+
contentParent = (ViewGroup) (fragment.getView().getParent());
40+
} else if (activityOrFragmentOrView instanceof View) {
41+
View view = (View) activityOrFragmentOrView;
42+
contentParent = (ViewGroup) (view.getParent());
43+
context = view.getContext();
44+
} else {
45+
throw new IllegalArgumentException("the argument's type must be Fragment or Activity: init(context)");
46+
}
47+
int childCount = contentParent.getChildCount();
48+
//get contentParent
49+
int index = 0;
50+
View oldContent;
51+
if (activityOrFragmentOrView instanceof View) {
52+
oldContent = (View) activityOrFragmentOrView;
53+
for (int i = 0; i < childCount; i++) {
54+
if (contentParent.getChildAt(i) == oldContent) {
55+
index = i;
56+
break;
57+
}
58+
}
59+
} else {
60+
oldContent = contentParent.getChildAt(0);
61+
}
62+
contentParent.removeView(oldContent);
63+
//setup content layout
64+
LoadingAndRetryLayout loadingAndRetryLayout = new LoadingAndRetryLayout(context);
65+
66+
ViewGroup.LayoutParams lp = oldContent.getLayoutParams();
67+
contentParent.addView(loadingAndRetryLayout, index, lp);
68+
loadingAndRetryLayout.setContentView(oldContent);
69+
// setup loading,retry,empty layout
70+
setupLoadingLayout(listener, loadingAndRetryLayout);
71+
setupRetryLayout(listener, loadingAndRetryLayout);
72+
setupEmptyLayout(listener, loadingAndRetryLayout);
73+
//callback
74+
listener.setRetryEvent(loadingAndRetryLayout.getRetryView());
75+
listener.setLoadingEvent(loadingAndRetryLayout.getLoadingView());
76+
listener.setEmptyEvent(loadingAndRetryLayout.getEmptyView());
77+
mLoadingAndRetryLayout = loadingAndRetryLayout;
78+
}
79+
80+
private void setupEmptyLayout(OnLoadingAndRetryListener listener, LoadingAndRetryLayout loadingAndRetryLayout) {
81+
if (listener.isSetEmptyLayout()) {
82+
int layoutId = listener.generateEmptyLayoutId();
83+
if (layoutId != NO_LAYOUT_ID) {
84+
loadingAndRetryLayout.setEmptyView(layoutId);
85+
} else {
86+
loadingAndRetryLayout.setEmptyView(listener.generateEmptyLayout());
87+
}
88+
} else {
89+
if (BASE_EMPTY_LAYOUT_ID != NO_LAYOUT_ID)
90+
loadingAndRetryLayout.setEmptyView(BASE_EMPTY_LAYOUT_ID);
91+
}
92+
}
93+
94+
private void setupLoadingLayout(OnLoadingAndRetryListener listener, LoadingAndRetryLayout loadingAndRetryLayout) {
95+
if (listener.isSetLoadingLayout()) {
96+
int layoutId = listener.generateLoadingLayoutId();
97+
if (layoutId != NO_LAYOUT_ID) {
98+
loadingAndRetryLayout.setLoadingView(layoutId);
99+
} else {
100+
loadingAndRetryLayout.setLoadingView(listener.generateLoadingLayout());
101+
}
102+
} else {
103+
if (BASE_LOADING_LAYOUT_ID != NO_LAYOUT_ID)
104+
loadingAndRetryLayout.setLoadingView(BASE_LOADING_LAYOUT_ID);
105+
}
106+
}
107+
108+
private void setupRetryLayout(OnLoadingAndRetryListener listener, LoadingAndRetryLayout loadingAndRetryLayout) {
109+
if (listener.isSetRetryLayout()) {
110+
int layoutId = listener.generateRetryLayoutId();
111+
if (layoutId != NO_LAYOUT_ID) {
112+
loadingAndRetryLayout.setLoadingView(layoutId);
113+
} else {
114+
loadingAndRetryLayout.setLoadingView(listener.generateRetryLayout());
115+
}
116+
} else {
117+
if (BASE_RETRY_LAYOUT_ID != NO_LAYOUT_ID)
118+
loadingAndRetryLayout.setRetryView(BASE_RETRY_LAYOUT_ID);
119+
}
120+
}
121+
122+
public static LoadingAndRetryManager generate(Object activityOrFragment, OnLoadingAndRetryListener listener) {
123+
return new LoadingAndRetryManager(activityOrFragment, listener);
124+
}
125+
126+
public void showLoading() {
127+
mLoadingAndRetryLayout.showLoading();
128+
}
129+
130+
public void showRetry() {
131+
mLoadingAndRetryLayout.showRetry();
132+
}
133+
134+
public void showContent() {
135+
mLoadingAndRetryLayout.showContent();
136+
}
137+
138+
public void showEmpty() {
139+
mLoadingAndRetryLayout.showEmpty();
140+
}
141+
}

0 commit comments

Comments
 (0)