Snackbar stack will always show only the last snackbar

戏子无情 提交于 2020-06-09 05:22:47

问题


The Snackbar.make(...).show() will only show the last snackbar unlike toast the other snackbars are gone.

for(int i = 1;i < 11;i++) {  
   Snackbar.make(..., "number: " + i, Snackbar.LENGTH_SHORT).show();
}

Sometimes when the snackbar should show after network task and one should show before network task, the seconds snackbar is never shown.

Im using a coordinatorlayout.


回答1:


Please refer the Android components docs:

Snackbars automatically time out from the screen. For usability reasons, snackbars should not contain the only way to access a core use case. They should not be persistent or be stacked, as they are above other elements on screen (Source)




回答2:


I implemented now my own queue:

protected ArrayList<Snackbar> mSnackbarList = new ArrayList<>();

protected Snackbar.Callback mCallback = new Snackbar.Callback() {
    @Override
    public void onDismissed(Snackbar snackbar, int event) {
        mSnackbarList.remove(snackbar);
        if (mSnackbarList.size() > 0)
           displaySnackbar(mSnackbarList.get(0));
    }
};

public void addQueue(Snackbar snackbar){
    setLayoutParams(snackbar);
    snackbar.setCallback(mCallback);
    boolean first = mSnackbarList.size() == 0;
    mSnackbarList.add(snackbar);
    if(first)
        displaySnackbar(snackbar);
}

public void displaySnackbar(Snackbar snackbar){
    snackbar.show();
}


来源:https://stackoverflow.com/questions/34150982/snackbar-stack-will-always-show-only-the-last-snackbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!