Snackbar(来自“ com.android.support:design:23.2.1”)支持多种类型的关闭操作。您可以使用event创建一个简单的过滤器,例如以下示例:
Snackbar.make(view, wornMessage, Snackbar.LENGTH_LONG).setActionTextColor(context.getResources().getColor(R.color.primary))
.setCallback(new Snackbar.Callback() {
@Override
public void onShown(Snackbar snackbar) {
super.onShown(snackbar);
// when snackbar is showing
}
@Override
public void onDismissed(Snackbar snackbar, int event) {
super.onDismissed(snackbar, event);
if (event != DISMISS_EVENT_ACTION) {
//will be true if user not click on Action button (for example: manual dismiss, dismiss by swipe
}
}
})
.setAction("Undo, view1 -> {
// if user click on Action button
}).show();
Snackbar的解雇类型:
/** Indicates that the Snackbar was dismissed via a swipe.*/
public static final int DISMISS_EVENT_SWIPE = 0;
/** Indicates that the Snackbar was dismissed via an action click.*/
public static final int DISMISS_EVENT_ACTION = 1;
/** Indicates that the Snackbar was dismissed via a timeout.*/
public static final int DISMISS_EVENT_TIMEOUT = 2;
/** Indicates that the Snackbar was dismissed via a call to {@link #dismiss()}.*/
public static final int DISMISS_EVENT_MANUAL = 3;
/** Indicates that the Snackbar was dismissed from a new Snackbar being shown.*/
public static final int DISMISS_EVENT_CONSECUTIVE = 4;
PS在示例代码中使用了lambda表达式(由RetroLambda提供)
Snackbar
对Google进行“撤消”操作违反了Google材料设计指南。