Android key controls Snackbar use method detailed _android

Source: Internet
Author: User

Snackbar is an important control in designsupportlibrary, used to prompt some key information below the interface, unlike toast where Snackbar allows the user to swipe right to remove it, while Also allows to set an action in the Snackbar, when the user clicks the button inside the Snackbar, can do some operations, so, the function is absolutely very powerful.

Construction of Snackbar:

Parameters are parent containers, prompting information, duration public static snackbar make (@NonNull view view, @NonNull charsequence text, @Duration int Duration) 

Common methods of Snackbar:

Used to set an action for Snackbar, click to callback Onclicklistener in the OnClick method public Snackbar setaction (charsequence text, final View.onclicklistener listener) 
//For setting action font Color public snackbar setactiontextcolor (@ColorInt int color) 
// The font of the settings hint public snackbar setText (@NonNull charsequence message) 
//Display snackbarpublic void Show () 
// Clear snackbarpublic void Dismiss () 
//set callback, such as ondismissed or Onshownpublic snackbar setcallback (Callback Callback) 

Simple to use is not to say, here directly to set the action and set callback
requirements: to implement a undo operation, the user clicks the list of each item in the Delete button, delete (the database) corresponding row, and then pop-up Snackbar prompts whether to undo, if the choice is, then restore the deleted row, otherwise delete the row, the effect is as follows

idea:
① the line we want to delete first, then delete the row when we click the Delete button, remove the data from the source, and then refresh
② pop-up Snackbar, set action, if yes, insert data into the source, refresh
③ sets callback, listens for Snackbar vanishing event, deletes data from SQLite if it is not clicked action
Note: Why do you want to set up this monitoring? Because if you do not listen, you need to access the database at the time of deletion, Undo is the time to insert the database, so the entry position in the list will change

We can listen to the Delete button in the adapter code as follows:

Delete.setonclicklistener (New View.onclicklistener () {
   @Override public
   void OnClick (View v) {
    Backupmap = Mdata.get (position);
    Mdata.remove (position);
    Notifydatasetchanged ();
    Snackbar Snackbar = Snackbar.make (Root, "undo deletion?) ", Snackbar.length_long);
    Snackbar.setaction ("YES", new Myonclicklistener (position));
    Snackbar.setcallback (New Mycallback ());
    Snackbar.show ();
   }
  );

If you click the Delete button, we back up the data, then delete the data source mdata the corresponding map, and then refresh the interface. Then line 7th constructs a snackbar, asks whether to undo the deletion, and sets the duration to long so that the user does not finish reading the text Snackbar disappears. Line 8th set the click Action, the message is "YES", and provides the click of the Listener. The 9th line sets the callback to determine the vanishing state of the Snackbar. The 10th line will directly show Snackbar.

Here's the Click event for the action:

Private class Myonclicklistener implements View.onclicklistener {
  private final int position;

  public Myonclicklistener (int position) {
   this.position = position;
  }

  @Override public
  void OnClick (View v) {
   mdata.add (position, backupmap);
   Notifydatasetchanged ();
  }
 

If you click on the action, use the backed-up data to restore
Look below callback:

Private class Mycallback extends Snackbar.callback {
  @Override public
  void ondismissed (Snackbar snackbar, int Event) {
   super.ondismissed (Snackbar, event);
   if (event = = Dismiss_event_swipe | | | event = = Dismiss_event_timeout | | | event = =
     dismiss_event_consecutive) {
    MDA Tabase.execsql ("Delete from tally where id=?", New String[]{backupmap.get
      ("id"). toString ()});
 

Here we see that we rewrite the ondismissed method, when the snackbar disappears, we call this method, we first judge the disappearing type, if you click on the action, you do not have to delete the data in the database, otherwise the database will be deleted.
Constants for Snackbar disappearing types:

/** indicates that the Snackbar is dismissed via a swipe.*/public
static final int dismiss_event_swipe = 0;
/** indicates that the Snackbar is dismissed via a action click.*/public
static final int dismiss_event_action = 1;
/** indicates that the Snackbar is dismissed via a timeout.*/public
static final int dismiss_event_timeout = 2;
/** indicates that the Snackbar is dismissed via a call to {@link #dismiss ()}.*/public
static final int dismiss _event_manual = 3;
/** indicates that the Snackbar is dismissed from a new snackbar being public
static final int shown.*/ _consecutive = 4; 


As you can see, from 0 to 4 are sliding clear, click Action, duration end, call dismiss method, and new Snackbar generation
Because every time the snackbar disappears is called the dismiss method, so we only need to determine whether the state is sliding clear, duration of the end or new snackbar generated, if it is, the user does not need to undo, then the database to delete.
Finally, if you use Snackbar, remember to add dependencies!!

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.