什么时候使用Snackbar isQueued()? (如何排队小吃店)

时间:2015-10-21 16:38:59

标签: android android-snackbar

如果我连续多次调用小吃店,则只显示最后一个小吃栏项目。

e.g。使用下面的代码,只显示第3项。对于第1项和第2项,似乎忽略了Snackbar.LENGTH_LONG(并设置为零?)。

Snackbar.make(view, "Item 1", Snackbar.LENGTH_LONG).show();
Snackbar.make(view, "Item 2", Snackbar.LENGTH_LONG).show();
Snackbar.make(view, "Item 3", Snackbar.LENGTH_LONG).show();

但在google documents中,我发现可以对邮件进行排队。

public boolean isShownOrQueued ()

Returns whether this Snackbar is currently being shown,
or is queued to be shown next.

那么我们如何实际排队小吃店呢?

5 个答案:

答案 0 :(得分:6)

这是一个解决您问题的部分代码段,尽管可能没有 是正确的做事方式:

//using a queue to pass string to the snackbar
Queue<String> myQueue = new LinkedList<String>();
myQueue.offer("item 1");
myQueue.offer("item 2");
myQueue.offer("item 3");
displaysnack(myQueue, view);

public void displaysnack(final Queue dQueue, final View view){
    Snackbar.make(view, (String)dQueue.poll(), Snackbar.LENGTH_LONG).setCallback(new Snackbar.Callback() {
        @Override
        public void onDismissed(Snackbar snackbar, int event) {
            switch (event) {
                case Snackbar.Callback.DISMISS_EVENT_ACTION:
                    Toast.makeText(getApplicationContext(), "Clicked the action", Toast.LENGTH_LONG).show();
                    break;
                //once the timeout expires, display the next one in the queue.
                case Snackbar.Callback.DISMISS_EVENT_TIMEOUT:
                    Toast.makeText(getApplicationContext(), "Showing: "+ (dQueue.size()), Toast.LENGTH_SHORT).show();
                    if (dQueue.size()>0){displaysnack(dQueue, view);}
                    break;
                case Snackbar.Callback.DISMISS_EVENT_CONSECUTIVE:
                    //Toast.makeText(getApplicationContext(), "Multiple Shown", Toast.LENGTH_SHORT).show();
                    break;
            }
        }

答案 1 :(得分:4)

I also needed to implement a queue of snackbars but did not find ready solution. So I decided to implement it on my own. You can try it https://github.com/AntonyGolovin/FluentSnackbar.

Just call important() on Builder and it will be added to the queue.

答案 2 :(得分:3)

我也实施了自己的,可能不是最适合我的需求。它在C#中用于 Xamarin

public class SnackbarManager : Snackbar.Callback
{
    List<Snackbar> snackbarsWaiting;
    List<Snackbar> snackbarsHolding;
    public SnackbarManager()
    {
        snackbarsWaiting = new List<Snackbar>();
        snackbarsHolding = new List<Snackbar>();
    }

    public void AddToQueue(Snackbar snackbar)
    {
        if (snackbar.Duration == Snackbar.LengthIndefinite) snackbar.SetDuration(Snackbar.LengthLong);
        snackbar.SetCallback(this);
        if (snackbarsWaiting.Count > 0 && snackbarsWaiting[0].IsShown) snackbarsHolding.Add(snackbar);
        else snackbarsWaiting.Add(snackbar);
    }

    public void Show()
    {
        if (snackbarsWaiting.Count > 0 && !snackbarsWaiting[0].IsShown)
            snackbarsWaiting[0].Show();
    }

    public override void OnDismissed(Snackbar snackbar, int evt)
    {
        base.OnDismissed(snackbar, evt);
        snackbarsWaiting.Remove(snackbar);
        if (snackbarsHolding.Count > 0)
        {
            snackbarsWaiting.AddRange(snackbarsHolding);
            snackbarsHolding.Clear();
        }
        if (snackbarsWaiting.Count > 0) snackbarsWaiting[0].Show();
    }

}

答案 3 :(得分:2)

我也遇到了这个问题,这是我的解决方案。

static List<Snackbar> snackBarList = new ArrayList<>();

public static void mySnackBar(CoordinatorLayout coordinatorLayout, String s,boolean queued) {

    Snackbar snackbar = Snackbar.make(coordinatorLayout, s, Snackbar.LENGTH_SHORT);

    if (queued) {

//if true set onDismiss CallBack

        snackbar.setCallback(new Snackbar.Callback()
        {          
            @Override
            public void onDismissed(Snackbar currentSnackbar, int event) {
                super.onDismissed(currentSnackbar, event);

//first remove current snackBar in List, then if List not empty show the first one

                snackBarList.remove(currentSnackbar);
                if (snackBarList.size() > 0) 
                    snackBarList.get(0).show();

            }

        });

//add (set callback) snackBar to List

        snackBarList.add(snackbar);

//the beginning

        if (snackBarList.size() == 1)
            snackBarList.get(0).show();

    } else snackbar.show();
}

答案 4 :(得分:2)

我写了一个这样做的库。它还包括progressBar。试一试https://github.com/tingyik90/snackprogressbar