Skip to content

Instantly share code, notes, and snippets.

@Folyd
Created September 15, 2015 09:01
Show Gist options
  • Select an option

  • Save Folyd/3892c1556b2ea68afda2 to your computer and use it in GitHub Desktop.

Select an option

Save Folyd/3892c1556b2ea68afda2 to your computer and use it in GitHub Desktop.
Toggle visibility helper class
import android.os.Handler;
/**
* A helper class can toggle target object visibility automatically.
* It is very useful for MediaController (or ActionBar) when play video in Activity.
*/
public abstract class ToggleVisibilityHelper {
/**
* Record the time while user lasted click.
*/
private long mToggleTime = 0L;
public void toggleVisibility() {
mToggleTime = System.currentTimeMillis();
if (isVisible()) {
hide();
} else {
show();
hideDelay();
}
}
/**
* A callback whether target object is visible currently.
*
* @return
*/
protected abstract boolean isVisible();
/**
* Whether the target object is still in playing state.
*
* @return
*/
protected boolean isPlaying() {
//The default value is true.
return true;
}
protected abstract void hide();
protected abstract void show();
protected void hideDelay() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (System.currentTimeMillis() - mToggleTime < 3000L || !isPlaying()) {
return;
}
if (isVisible()) {
hide();
}
}
}, 3500);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment