Created
September 15, 2015 09:01
-
-
Save Folyd/3892c1556b2ea68afda2 to your computer and use it in GitHub Desktop.
Toggle visibility helper class
This file contains hidden or 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
| 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