gpt4 book ai didi

android - 使用 SnackBar 实例反复显示

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:00 25 4
gpt4 key购买 nike

步骤

我可以这样保存 SnackBar 实例:

mSnackBar = Snackbar.make(view, R.string.snack_text, Snackbar.LENGTH_INDEFINITE);

而且对于第一次来说,使用这个很容易显示:mSnackBar.show();


问题

但是在我使用这个清除这个Snack之后:mSnackBar.dismiss()

它不会在LOLLIPOP 设备中再次显示,
它会在JELLYBEANshow()) > 模拟器这是预期的行为。


问题

请帮我找出LOLLIPOP 设备在这个过程中有什么问题或缺失?

最佳答案

查看源代码,我可以看到“关闭 snackbar ”将使当前的 snackbar 无效。

Source Code - SnackBar

public void dismiss(Callback callback, int event) {
synchronized (mLock) {
if (isCurrentSnackbarLocked(callback)) {
cancelSnackbarLocked(mCurrentSnackbar, event);
} else if (isNextSnackbarLocked(callback)) {
cancelSnackbarLocked(mNextSnackbar, event);
}
}
}

/**
* Should be called when a Snackbar is no longer displayed. This is after any exit
* animation has finished.
*/
public void onDismissed(Callback callback) {
synchronized (mLock) {
if (isCurrentSnackbarLocked(callback)) {
// If the callback is from a Snackbar currently show, remove it and show a new one
mCurrentSnackbar = null;
if (mNextSnackbar != null) {
showNextSnackbarLocked();
}
}
}
}

所以当你在同一个实例上做一个节目时,这段代码将运行

public void show(int duration, Callback callback) {
synchronized (mLock) {
if (isCurrentSnackbarLocked(callback)) {
// Means that the callback is already in the queue. We'll just update the duration
mCurrentSnackbar.duration = duration;

// If this is the Snackbar 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 SnackbarRecord(duration, callback);
}

if (mCurrentSnackbar != null && cancelSnackbarLocked(mCurrentSnackbar,
Snackbar.Callback.DISMISS_EVENT_CONSECUTIVE)) {
// If we currently have a Snackbar, try and cancel it and wait in line
return;
} else {
// Clear out the current snackbar
mCurrentSnackbar = null;
// Otherwise, just show it now
showNextSnackbarLocked();
}
}
}

如果它为 null,则不会显示 snackbar。

Solution

您不应在 SnackBar 上调用 dismiss,它会在持续时间到期或单击操作按钮时自动隐藏。只需再次调用 show 方法而不首先调用 dismiss 即可再次显示 SnackBar。

关于android - 使用 SnackBar 实例反复显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34555883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com