How to dismiss a Snackbar using it's own Action button?

前端 未结 6 1225
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 11:53

Android design support library now includes support for Snackbar.

I\'ve used the following code to create one:

Snackbar.make(findViewById(R.id.root_l         


        
6条回答
  •  情书的邮戳
    2020-12-13 12:09

    Snackbar (from 'com.android.support:design:23.2.1') support many types of dismiss action. You can create a simple filter by using event, such as in this example:

    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's dismiss types:

    /** 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;
    

    P.S. In sample code used lambda expressions (by RetroLambda)

提交回复
热议问题