-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72bf645
commit 4866c05
Showing
3 changed files
with
408 additions
and
0 deletions.
There are no files selected for viewing
211 changes: 211 additions & 0 deletions
211
...dutil/src/main/java/com/siberiadante/androidutil/view/pageview/LoadingAndRetryLayout.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
package com.siberiadante.androidutil.view.pageview; | ||
|
||
import android.content.Context; | ||
import android.os.Looper; | ||
import android.util.AttributeSet; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.widget.FrameLayout; | ||
|
||
import com.siberiadante.androidutil.util.SDLogUtil; | ||
|
||
|
||
public class LoadingAndRetryLayout extends FrameLayout { | ||
private View mLoadingView; | ||
private View mRetryView; | ||
private View mContentView; | ||
private View mEmptyView; | ||
private LayoutInflater mInflater; | ||
|
||
private static final String TAG = LoadingAndRetryLayout.class.getSimpleName(); | ||
|
||
|
||
public LoadingAndRetryLayout(Context context, AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
mInflater = LayoutInflater.from(context); | ||
} | ||
|
||
|
||
public LoadingAndRetryLayout(Context context, AttributeSet attrs) { | ||
this(context, attrs, -1); | ||
} | ||
|
||
public LoadingAndRetryLayout(Context context) { | ||
this(context, null); | ||
} | ||
|
||
private boolean isMainThread() { | ||
return Looper.myLooper() == Looper.getMainLooper(); | ||
} | ||
|
||
public void showLoading() { | ||
if (isMainThread()) { | ||
showView(mLoadingView); | ||
} else { | ||
post(new Runnable() { | ||
@Override | ||
public void run() { | ||
showView(mLoadingView); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public void showRetry() { | ||
if (isMainThread()) { | ||
showView(mRetryView); | ||
} else { | ||
post(new Runnable() { | ||
@Override | ||
public void run() { | ||
showView(mRetryView); | ||
} | ||
}); | ||
} | ||
|
||
} | ||
|
||
public void showContent() { | ||
if (isMainThread()) { | ||
showView(mContentView); | ||
} else { | ||
post(new Runnable() { | ||
@Override | ||
public void run() { | ||
showView(mContentView); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public void showEmpty() { | ||
if (isMainThread()) { | ||
showView(mEmptyView); | ||
} else { | ||
post(new Runnable() { | ||
@Override | ||
public void run() { | ||
showView(mEmptyView); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
|
||
private void showView(View view) { | ||
if (view == null) return; | ||
|
||
if (view == mLoadingView) { | ||
mLoadingView.setVisibility(View.VISIBLE); | ||
if (mRetryView != null) | ||
mRetryView.setVisibility(View.GONE); | ||
if (mContentView != null) | ||
mContentView.setVisibility(View.GONE); | ||
if (mEmptyView != null) | ||
mEmptyView.setVisibility(View.GONE); | ||
} else if (view == mRetryView) { | ||
mRetryView.setVisibility(View.VISIBLE); | ||
if (mLoadingView != null) | ||
mLoadingView.setVisibility(View.GONE); | ||
if (mContentView != null) | ||
mContentView.setVisibility(View.GONE); | ||
if (mEmptyView != null) | ||
mEmptyView.setVisibility(View.GONE); | ||
} else if (view == mContentView) { | ||
mContentView.setVisibility(View.VISIBLE); | ||
if (mLoadingView != null) | ||
mLoadingView.setVisibility(View.GONE); | ||
if (mRetryView != null) | ||
mRetryView.setVisibility(View.GONE); | ||
if (mEmptyView != null) | ||
mEmptyView.setVisibility(View.GONE); | ||
} else if (view == mEmptyView) { | ||
mEmptyView.setVisibility(View.VISIBLE); | ||
if (mLoadingView != null) | ||
mLoadingView.setVisibility(View.GONE); | ||
if (mRetryView != null) | ||
mRetryView.setVisibility(View.GONE); | ||
if (mContentView != null) | ||
mContentView.setVisibility(View.GONE); | ||
} | ||
|
||
|
||
} | ||
|
||
public View setContentView(int layoutId) { | ||
return setContentView(mInflater.inflate(layoutId, this, false)); | ||
} | ||
|
||
public View setLoadingView(int layoutId) { | ||
return setLoadingView(mInflater.inflate(layoutId, this, false)); | ||
} | ||
|
||
public View setEmptyView(int layoutId) { | ||
return setEmptyView(mInflater.inflate(layoutId, this, false)); | ||
} | ||
|
||
public View setRetryView(int layoutId) { | ||
return setRetryView(mInflater.inflate(layoutId, this, false)); | ||
} | ||
|
||
public View setLoadingView(View view) { | ||
View loadingView = mLoadingView; | ||
if (loadingView != null) { | ||
SDLogUtil.w(TAG, "you have already set a loading view and would be instead of this new one."); | ||
} | ||
removeView(loadingView); | ||
addView(view); | ||
mLoadingView = view; | ||
return mLoadingView; | ||
} | ||
|
||
public View setEmptyView(View view) { | ||
View emptyView = mEmptyView; | ||
if (emptyView != null) { | ||
SDLogUtil.w(TAG, "you have already set a empty view and would be instead of this new one."); | ||
} | ||
removeView(emptyView); | ||
addView(view); | ||
mEmptyView = view; | ||
return mEmptyView; | ||
} | ||
|
||
public View setRetryView(View view) { | ||
View retryView = mRetryView; | ||
if (retryView != null) { | ||
SDLogUtil.w(TAG, "you have already set a retry view and would be instead of this new one."); | ||
} | ||
removeView(retryView); | ||
addView(view); | ||
mRetryView = view; | ||
return mRetryView; | ||
|
||
} | ||
|
||
public View setContentView(View view) { | ||
View contentView = mContentView; | ||
if (contentView != null) { | ||
SDLogUtil.w(TAG, "you have already set a retry view and would be instead of this new one."); | ||
} | ||
removeView(contentView); | ||
addView(view); | ||
mContentView = view; | ||
return mContentView; | ||
} | ||
|
||
public View getRetryView() { | ||
return mRetryView; | ||
} | ||
|
||
public View getLoadingView() { | ||
return mLoadingView; | ||
} | ||
|
||
public View getContentView() { | ||
return mContentView; | ||
} | ||
|
||
public View getEmptyView() { | ||
return mEmptyView; | ||
} | ||
} |
141 changes: 141 additions & 0 deletions
141
...util/src/main/java/com/siberiadante/androidutil/view/pageview/LoadingAndRetryManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package com.siberiadante.androidutil.view.pageview; | ||
|
||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.support.v4.app.Fragment; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
|
||
public class LoadingAndRetryManager { | ||
public static final int NO_LAYOUT_ID = 0; | ||
public static int BASE_LOADING_LAYOUT_ID = NO_LAYOUT_ID; | ||
public static int BASE_RETRY_LAYOUT_ID = NO_LAYOUT_ID; | ||
public static int BASE_EMPTY_LAYOUT_ID = NO_LAYOUT_ID; | ||
|
||
public LoadingAndRetryLayout mLoadingAndRetryLayout; | ||
|
||
|
||
public OnLoadingAndRetryListener DEFAULT_LISTENER = new OnLoadingAndRetryListener() { | ||
@Override | ||
public void setRetryEvent(View retryView) { | ||
|
||
} | ||
}; | ||
|
||
|
||
public LoadingAndRetryManager(Object activityOrFragmentOrView, OnLoadingAndRetryListener listener) { | ||
if (listener == null) listener = DEFAULT_LISTENER; | ||
|
||
ViewGroup contentParent = null; | ||
Context context; | ||
if (activityOrFragmentOrView instanceof Activity) { | ||
Activity activity = (Activity) activityOrFragmentOrView; | ||
context = activity; | ||
contentParent = (ViewGroup) activity.findViewById(android.R.id.content); | ||
} else if (activityOrFragmentOrView instanceof Fragment) { | ||
Fragment fragment = (Fragment) activityOrFragmentOrView; | ||
context = fragment.getActivity(); | ||
contentParent = (ViewGroup) (fragment.getView().getParent()); | ||
} else if (activityOrFragmentOrView instanceof View) { | ||
View view = (View) activityOrFragmentOrView; | ||
contentParent = (ViewGroup) (view.getParent()); | ||
context = view.getContext(); | ||
} else { | ||
throw new IllegalArgumentException("the argument's type must be Fragment or Activity: init(context)"); | ||
} | ||
int childCount = contentParent.getChildCount(); | ||
//get contentParent | ||
int index = 0; | ||
View oldContent; | ||
if (activityOrFragmentOrView instanceof View) { | ||
oldContent = (View) activityOrFragmentOrView; | ||
for (int i = 0; i < childCount; i++) { | ||
if (contentParent.getChildAt(i) == oldContent) { | ||
index = i; | ||
break; | ||
} | ||
} | ||
} else { | ||
oldContent = contentParent.getChildAt(0); | ||
} | ||
contentParent.removeView(oldContent); | ||
//setup content layout | ||
LoadingAndRetryLayout loadingAndRetryLayout = new LoadingAndRetryLayout(context); | ||
|
||
ViewGroup.LayoutParams lp = oldContent.getLayoutParams(); | ||
contentParent.addView(loadingAndRetryLayout, index, lp); | ||
loadingAndRetryLayout.setContentView(oldContent); | ||
// setup loading,retry,empty layout | ||
setupLoadingLayout(listener, loadingAndRetryLayout); | ||
setupRetryLayout(listener, loadingAndRetryLayout); | ||
setupEmptyLayout(listener, loadingAndRetryLayout); | ||
//callback | ||
listener.setRetryEvent(loadingAndRetryLayout.getRetryView()); | ||
listener.setLoadingEvent(loadingAndRetryLayout.getLoadingView()); | ||
listener.setEmptyEvent(loadingAndRetryLayout.getEmptyView()); | ||
mLoadingAndRetryLayout = loadingAndRetryLayout; | ||
} | ||
|
||
private void setupEmptyLayout(OnLoadingAndRetryListener listener, LoadingAndRetryLayout loadingAndRetryLayout) { | ||
if (listener.isSetEmptyLayout()) { | ||
int layoutId = listener.generateEmptyLayoutId(); | ||
if (layoutId != NO_LAYOUT_ID) { | ||
loadingAndRetryLayout.setEmptyView(layoutId); | ||
} else { | ||
loadingAndRetryLayout.setEmptyView(listener.generateEmptyLayout()); | ||
} | ||
} else { | ||
if (BASE_EMPTY_LAYOUT_ID != NO_LAYOUT_ID) | ||
loadingAndRetryLayout.setEmptyView(BASE_EMPTY_LAYOUT_ID); | ||
} | ||
} | ||
|
||
private void setupLoadingLayout(OnLoadingAndRetryListener listener, LoadingAndRetryLayout loadingAndRetryLayout) { | ||
if (listener.isSetLoadingLayout()) { | ||
int layoutId = listener.generateLoadingLayoutId(); | ||
if (layoutId != NO_LAYOUT_ID) { | ||
loadingAndRetryLayout.setLoadingView(layoutId); | ||
} else { | ||
loadingAndRetryLayout.setLoadingView(listener.generateLoadingLayout()); | ||
} | ||
} else { | ||
if (BASE_LOADING_LAYOUT_ID != NO_LAYOUT_ID) | ||
loadingAndRetryLayout.setLoadingView(BASE_LOADING_LAYOUT_ID); | ||
} | ||
} | ||
|
||
private void setupRetryLayout(OnLoadingAndRetryListener listener, LoadingAndRetryLayout loadingAndRetryLayout) { | ||
if (listener.isSetRetryLayout()) { | ||
int layoutId = listener.generateRetryLayoutId(); | ||
if (layoutId != NO_LAYOUT_ID) { | ||
loadingAndRetryLayout.setLoadingView(layoutId); | ||
} else { | ||
loadingAndRetryLayout.setLoadingView(listener.generateRetryLayout()); | ||
} | ||
} else { | ||
if (BASE_RETRY_LAYOUT_ID != NO_LAYOUT_ID) | ||
loadingAndRetryLayout.setRetryView(BASE_RETRY_LAYOUT_ID); | ||
} | ||
} | ||
|
||
public static LoadingAndRetryManager generate(Object activityOrFragment, OnLoadingAndRetryListener listener) { | ||
return new LoadingAndRetryManager(activityOrFragment, listener); | ||
} | ||
|
||
public void showLoading() { | ||
mLoadingAndRetryLayout.showLoading(); | ||
} | ||
|
||
public void showRetry() { | ||
mLoadingAndRetryLayout.showRetry(); | ||
} | ||
|
||
public void showContent() { | ||
mLoadingAndRetryLayout.showContent(); | ||
} | ||
|
||
public void showEmpty() { | ||
mLoadingAndRetryLayout.showEmpty(); | ||
} | ||
} |
Oops, something went wrong.