Skip to content

Instantly share code, notes, and snippets.

@chrisjenx
Created May 21, 2014 09:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisjenx/3443af5361ab7982360e to your computer and use it in GitHub Desktop.
Save chrisjenx/3443af5361ab7982360e to your computer and use it in GitHub Desktop.
Similar to the LoadingProgressSpinner which doesnt flicker if you change stages quickly.
public abstract class ContentLoadingFrameLayout extends FrameLayout {
private static final int MIN_SHOW_TIME = 200; // ms
private static final int MIN_DELAY = 200; // ms
private ProgressBar mProgressBar;
private View mContent;
private long mStartTime = -1;
private boolean mPostedHide = false;
private boolean mPostedShow = false;
private boolean mDismissed = false;
private final Runnable mDelayedHide = new Runnable() {
@Override
public void run() {
mPostedHide = false;
mStartTime = -1;
hide();
}
};
private final Runnable mDelayedShow = new Runnable() {
@Override
public void run() {
mPostedShow = false;
if (!mDismissed) {
mStartTime = System.currentTimeMillis();
show();
}
}
};
public ContentLoadingFrameLayout(Context context) {
super(context);
init(context);
}
public ContentLoadingFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public ContentLoadingFrameLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
LayoutInflater.from(context).inflate(R.layout.layout_content_loading_include, this, true);
mProgressBar = ButterKnife.findById(this, R.id.content_loading_progress_bar);
mProgressBar.setAlpha(0f);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
if (mContent == null) {
for (int i = 0; i < getChildCount(); i++) {
if (getChildAt(i) != mProgressBar) {
mContent = getChildAt(i);
break;
}
}
}
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
removeCallbacks();
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
removeCallbacks();
}
private void removeCallbacks() {
removeCallbacks(mDelayedHide);
removeCallbacks(mDelayedShow);
}
/**
* Hide the progress view if it is visible. The progress view will not be
* hidden until it has been shown for at least a minimum show time. If the
* progress view was not yet visible, cancels showing the progress view.
*/
public void hideLoading() {
mDismissed = true;
removeCallbacks(mDelayedShow);
long diff = System.currentTimeMillis() - mStartTime;
if (diff >= MIN_SHOW_TIME || mStartTime == -1) {
// The progress spinner has been shown long enough
// OR was not shown yet. If it wasn't shown yet,
// it will just never be shown.
hide();
} else {
// The progress spinner is shown, but not long enough,
// so put a delayed message in to hide it when its been
// shown long enough.
if (!mPostedHide) {
postDelayed(mDelayedHide, MIN_SHOW_TIME - diff);
mPostedHide = true;
}
}
}
/**
* Show the progress view after waiting for a minimum delay. If
* during that time, hide() is called, the view is never made visible.
*/
public void showLoading() {
// Reset the start time.
mStartTime = -1;
mDismissed = false;
removeCallbacks(mDelayedHide);
if (!mPostedShow) {
postDelayed(mDelayedShow, MIN_DELAY);
mPostedShow = true;
}
}
private void hide() {
mProgressBar.animate()
.alpha(0f)
.setDuration(MIN_DELAY)
.start();
if (mContent == null) return;
mContent.animate()
.alpha(1f)
.setDuration(MIN_DELAY)
.start();
}
private void show() {
mProgressBar.animate()
.alpha(1f)
.setDuration(MIN_DELAY)
.start();
if (mContent == null) return;
mContent.animate()
.alpha(0.2f)
.setDuration(MIN_DELAY)
.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment