Snackbar details of the Android Material Design series

Source: Internet
Author: User

Snackbar is a lightweight feedback component provided by Google Material design. Support to display a concise hint from the bottom of the layout, support manual swipe to cancel the operation, at the same time can only display a SnackBar.

how did that Snackbar be achieved? We mainly discuss the display logic of Snackbar, including: Delay disappears and only one snackbar display is supported at the same time

First, let's take a look at the two classes that Snackbar uses.

The Snackbarmanager is a singleton, using the form of a singleton to ensure that only one Snackbar is displayed at a time .

Shandler in Snackbar is used in Snackbarmanager.callback callback functions, and every Snackbar will implement Snackbarmanager.callback Callbacks, including show and Dimiss two methods. Snackbarmanager also uses this callback to differentiate between each of the different Snackbar.

Private Boolean iscurrentsnackbarlocked (Convinentsnackbarmanager.callback Callback) {    return Mcurrentsnackbar! = Null && Mcurrentsnackbar.issnackbar (callback);}

  

To make it easier for users to customize the behavior of Snackbar,Snackbar provides snackbar.callback, including the Snackbar display, and the behavior after it disappears. Users can set up this callback according to their business needs . The interface methods provided are as follows:

/** * Called when the given {@link Convinentsnackbar} have been dismissed, either through a time-out, * hav         ing been manually dismissed, or an action being clicked.         * * @param snackbar the snackbar which has been dismissed. * @param event The event which caused the dismissal.                 One of either: * {@link #DISMISS_EVENT_SWIPE}, {@link #DISMISS_EVENT_ACTION}, * {@link #DISMISS_EVENT_TIMEOUT}, {@link #DISMISS_EVENT_MANUAL} or * {@link #DISMISS_EVENT_CON         Secutive}. * @see Convinentsnackbar#dismiss () */public void ondismissed (Convinentsnackbar snackbar, @ConvinentSnackba r.callback.dismissevent int Event) {//empty}/** * Called when the given {@link Convin         Entsnackbar} is visible.         * * @param snackbar the snackbar which is now visible. * @see convinentsnackbar#show () */public void Onshown (COnvinentsnackbar snackbar) {//empty} 

  

snackbarmanager, in order to save each snackbar, snackbar callback, one is this duration. But snackbarmanager not use a queue to save each new generated snackbar. Only the current and the display will be saved. And the next rule is: in a snackbar display period (there is already a snackbar exist), will only put the last new snackbar. If you are new to a snackbar every time you click on the button, then 2.75s (Long_duration_ms), you clicked snackbarmanager will only show 1 times and n times. None of the others will be displayed. And the display of n Times is automatically called after the first display ends

public void Show (int duration, Convinentsnackbarmanager.callback Callback) {synchronized (MLock) {if ( Iscurrentsnackbarlocked (callback)) {//Means that the callback are already in the queue.                We ' ll just update the duration mcurrentsnackbar.duration = duration;                If This was the Convinentsnackbar currently being shown, call re-schedule it ' s//timeout                Mhandler.removecallbacksandmessages (Mcurrentsnackbar);                Scheduletimeoutlocked (Mcurrentsnackbar);            Return } else if (Isnextsnackbarlocked (callback)) {//We ' ll just update the duration Mnextsnackbar            . Duration = Duration; } else {//else, we need to create a new record and queue it Mnextsnackbar = new convinents            Nackbarmanager.snackbarrecord (Duration, callback); } if (Mcurrentsnackbar! = null && CancelsnackbarloCked (Mcurrentsnackbar, ConvinentSnackbar.Callback.DISMISS_EVENT_CONSECUTIVE)) {//If we            Currently has a convinentsnackbar, try and cancel it and wait in line return;                } else {//Clear out of the current snackbar Mcurrentsnackbar = null;            Otherwise, just show it Now shownextsnackbarlocked (); }        }    }

  

Snackbarmanager also has a handler, the handler is used to delay the disappearance of the animation. Each time a snackbar is generated, if duration is not set to length_indefinite(does not disappear), Snackbarmanager after the display, will use this handler sendmessagedelayed.

to delay sending a vanishing message. Handler will call Snackbarmanager.callback after receiving this message.

, through the Snackbarmanager callback to achieve the snackbar display and disappear.

Advantages of Snackbar:

Provides a more flexible and more user-friendly display method than toast.

Advantages of Snackbar:

1. The display position is too single, now only supports the bottom display.

2. The swipe operation is too single and now only supports left-to-right swipe cancellation.

A few changes have been made to Snackbar, and GitHub's project is Convinentsnackbar. The address is:

Https://github.com/stephen-wu-yuan/ConvinentSnackbar

The reorganization piece supports setting the slide direction and supports the top display.

Snackbar details of the Android Material Design series

Related Article

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.