How to use setDuration() method in SnackBar (Android Design Support Library)

前端 未结 7 1832
不知归路
不知归路 2020-12-23 16:09

From Documentation: parameter duration - either be one of the predefined lengths: LENGTH_SHORT, LENGTH_LONG, or a custom duration in milliseconds. But I can

相关标签:
7条回答
  • 2020-12-23 16:32

    Hello there give this external library a try https://github.com/nispok/snackbar. It is deprecated but it will easily solve your problem. It is moreover easy to implement. Before Support library i was using this library only for snackbars. Due to the duration problem of support library, i am happy to use this library only.

    0 讨论(0)
  • 2020-12-23 16:39

    Since 'com.android.support:design:22.2.1'

    you can set the duration of your Snackbar to LENGTH_INDEFINITEit will make the Snackbar shown until it is dismissed or another snackbar is shown.

    0 讨论(0)
  • 2020-12-23 16:40

    I have created a work around for this, i made a Class that sets snackbars with a custom duration using handler and postDelayed:

    public class SnackBarMaker {
    
    public static void snack(View content, String message, String actionText,  int actionTextColor, View.OnClickListener onClick){
        Snackbar.make(content, message, Snackbar.LENGTH_LONG)
                .setAction(actionText, onClick)
                .setActionTextColor(actionTextColor)
                .show();
    }
    
    public static void snackWithCustomTiming(View content, String message, int duration){
        final Snackbar snackbar = Snackbar.make(content, message, Snackbar.LENGTH_INDEFINITE);
        snackbar.show();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                snackbar.dismiss();
            }
        },duration);
    }
    }
    

    to use like this:

      //your duration
       int duration = 4000 
    SnackBarMaker.snackWithCustomTiming(getActivity().findViewById(android.R.id.content)
                                                   , getString(R.string.your_message), duration);
    
    0 讨论(0)
  • 2020-12-23 16:43

    It seems to be fixed in

    compile 'com.android.support:design:22.2.1'
    

    Only Lint shows it red underlined, but it works.

    0 讨论(0)
  • 2020-12-23 16:51

    Based on the implementation of Snackbar and SnackbarManager, I can confirm Eugene H's assessment: it's a bug. From SnackbarManager:

    private void scheduleTimeoutLocked(SnackbarRecord r) {
        mHandler.removeCallbacksAndMessages(r);
        mHandler.sendMessageDelayed(Message.obtain(mHandler, MSG_TIMEOUT, r),
                r.duration == Snackbar.LENGTH_LONG
                        ? LONG_DURATION_MS
                        : SHORT_DURATION_MS);
    }
    

    So, any value that is not LENGTH_LONG results in a short-duration snackbar.

    I have filed an issue about it.

    Edit: Has been fixed in revision 22.2.1. Check the release notes here

    The android docs have NOT been updated yet, but if you jump into the source code you'll notice that the parameter to the method setDuration(int duration) can either be one of LENGTH_SHORT, LENGTH_LONG, LENGTH_INDEFINITE or a custom duration in milliseconds

    0 讨论(0)
  • 2020-12-23 16:58

    Set the initial duration to LENGTH_INDEFINITE then set your custom duration afterwards:

    Snackbar
    .make(parentLayout, "Feed cat?", Snackbar.LENGTH_INDEFINITE)
    .setAction("Yes", snackOnClickListener)
    .setActionTextColor(Color.MAGENTA)
    .setDuration(8000)
    .show();
    

    EDIT

    Setting a period directly in milliseconds now works;

    Snackbar
    .make(parentLayout, "Feed cat?", 8000)
    .setAction("Yes", snackOnClickListener)
    .setActionTextColor(Color.MAGENTA)
    .show();
    
    0 讨论(0)
提交回复
热议问题