Skip to content

Instantly share code, notes, and snippets.

@sinergy
Created June 13, 2014 14:36
Show Gist options
  • Save sinergy/26005d6f6a9a049ce5f1 to your computer and use it in GitHub Desktop.
Save sinergy/26005d6f6a9a049ce5f1 to your computer and use it in GitHub Desktop.
This class allows a single click and prevents multiple clicks on the same button in rapid succession.
/**
* This class allows a single click and prevents multiple clicks on
* the same button in rapid succession. Setting unclickable is not enough
* because click events may still be queued up.
*
* Override onOneClick() to handle single clicks. Call reset() when you want to
* accept another click.
*/
public abstract class OnOneClickListener implements View.OnClickListener {
private boolean clickable = true;
@Override
public void onClick(View view) {
if (clickable) {
clickable = false;
onOnClick(view);
}
}
/**
* Override this function to handle clicks.
* reset() must be called after each click for this function to be called again
* @param view
*/
public abstract void onOnClick(View view);
/**
* Allows another click.
*/
public void reset() {
clickable = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment