Android Development Learning Path-snackbar usage experience

Source: Internet
Author: User

Snackbar is an important control in designsupportlibrary that is used to prompt some key information under 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 clicked on the Snackbar inside the button, you can do some operations, so, the function is absolutely very powerful.

Structure of the Snackbar:

Parameters are parent container, prompt information, duration
publicstaticint duration)

Common methods of Snackbar:

Used to set an action on Snackbar, and then callback the OnClick method in Onclicklistener
publicfinal View.onclicklistener listener)
Used to set the font color of the action
publicint color)
Set the font for the hint
public Snackbar setText (@NonNull charsequence message)
Show Snackbar
publicvoid Show ()
Clear Snackbar
publicvoid dismiss ()
Set callbacks, such as ondismissed or Onshown
public Snackbar setcallback (Callback Callback)

Simple is to use not to say, here directly to set the action and set callback

Requirements: To implement a revocation operation, the user clicks on each item in the list after the delete button, delete the corresponding row (in the database), and then pop up the Snackbar prompt is revoked, if you select Yes, then restore the deleted row, otherwise delete the row, the effect is as follows

Ideas:

① first to the row we want to delete the backup, and then click the Delete button when the row is deleted, the data from the source, and then refresh

② pops up Snackbar, sets the action, if you click Yes, inserts the data into the source, refreshes

③ set callback, listen for Snackbar disappear event, if not click action to disappear, then delete data from SQLite

Note: Why should I set up monitoring here? Because if you do not listen, you need to access the database at the time of deletion, it is time to insert the database, so that the location of the entries in the list will change

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

1Delete.setonclicklistener (NewView.onclicklistener () {2 @Override3              Public voidOnClick (View v) {4Backupmap =Mdata.get (position);5 Mdata.remove (position);6 notifydatasetchanged ();7Snackbar Snackbar = Snackbar.make (Root, "Do you want to undo the deletion?") ", Snackbar.length_long);8Snackbar.setaction ("YES",NewMyonclicklistener (position));9Snackbar.setcallback (Newmycallback ());Ten snackbar.show (); One             } A});

If you click the Delete button, we make a backup of the data, then delete the data source mdata the corresponding map, then refresh the interface. Then the 7th line 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. The 8th line sets the action of the click, prompting the message "YES" and providing a tap listener. The 9th line sets the callback to judge the disappearance state of Snackbar. The 10th line shows the Snackbar directly.

Here's the action's Click event:

1 Private classMyonclicklistenerImplementsView.onclicklistener {2         Private Final intposition;3 4          PublicMyonclicklistener (intposition) {5              This. Position =position;6         }7 8 @Override9          Public voidOnClick (View v) {Ten Mdata.add (position, backupmap); One notifydatasetchanged (); A         } -}

If you click Action, restore it with the backed up data

See below callback:

1 Private classMycallbackextendsSnackbar.callback {2 @Override3          Public voidOndismissed (Snackbar Snackbar,intevent) {4             Super. ondismissed (Snackbar, event);5             if(event = = Dismiss_event_swipe | | event = = Dismiss_event_timeout | | event = =6 dismiss_event_consecutive) {7Mdatabase.execsql ("Delete from tally where id=?",NewString[]{backupmap.get8("id"). toString ()});9             }Ten         } One}

See here we rewrite the ondismissed method, when the snackbar disappears, we will call back this method, we first determine the type of disappearance, if the action is clicked, you do not have to delete the data in the database, otherwise the database is deleted.

Constants that correspond to the types of Snackbar disappearing:

/**indicates the Snackbar was dismissed via a swipe.*/ Public Static Final intDismiss_event_swipe = 0;/**indicates that the Snackbar is dismissed via an action click.*/ Public Static Final intDismiss_event_action = 1;/**indicates that the Snackbar is dismissed via a timeout.*/ Public Static Final intDismiss_event_timeout = 2;/**indicates that the Snackbar is dismissed via a call to {@link#dismiss ()}.*/ Public Static Final intDismiss_event_manual = 3;/**indicates that the Snackbar is dismissed from a new Snackbar being shown.*/ Public Static Final intDismiss_event_consecutive = 4;

As you can see, from 0 to 4 are swipe clear, click Action, end of duration, call dismiss method, and have new Snackbar generated

Because every time Snackbar disappears is called the dismiss method, so we just need to determine whether the state at this time is sliding clear, the duration of the end or a new Snackbar generation, if so, prove that the user does not need to revoke, the database is deleted.

Finally, if you use Snackbar, remember to add dependencies!!

Android Development Learning Path-snackbar experience

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.