How to disable snackbar's swipe-to-dismiss behavior

后端 未结 6 1673
执念已碎
执念已碎 2020-12-05 18:52

Is there a way to prevent the user from dismissing a snackbar by swiping on it?

I have an app that shows a snack bar during network login, I want to avoid it to be d

6条回答
  •  孤街浪徒
    2020-12-05 19:29

    Snackbar now has actual support for this by using the setBehavior method. The great thing here is that before you would always lose some behaviors which are now preserved.

    Note that the package moved so you have to import the "new" Snackbar in the snackbar package.

    Snackbar.make(view, stringId, Snackbar.LENGTH_LONG)
        .setBehavior(new NoSwipeBehavior())
        .show();
    
    class NoSwipeBehavior extends BaseTransientBottomBar.Behavior {
    
        @Override
        public boolean canSwipeDismissView(View child) {
          return false;
        }
    }
    

提交回复
热议问题