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();
スナックバーの却下タイプ:
/** 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サンプルコードでは、ラムダ式を使用しました(RetroLambdaによる)