Android BottomSheetBehavior, how to disable snap? -


standard android bottomsheetbehavior has tree state: hidden, collapsed , expanded.

i want allow user "leave" bottom sheet between collapsed , expanded. now, default behavior, snap collapsed or expanded based closest. how should disable snap functionality?

i present way achievie such functionality view extending bottomsheetdialogfragment.

expanding:

first of overrive onresume:

@override public void onresume() {     super.onresume();     addglobalayoutlistener(getview()); }  private void addglobalayoutlistener(final view view) {     view.addonlayoutchangelistener(new view.onlayoutchangelistener() {         @override         public void onlayoutchange(view v, int left, int top, int right, int bottom, int oldleft, int oldtop, int oldright, int oldbottom) {             setpeekheight(v.getmeasuredheight());             v.removeonlayoutchangelistener(this);         }     }); }  public void setpeekheight(int peekheight) {     bottomsheetbehavior behavior = getbottomsheetbehaviour();     if (behavior == null) {         return;     }     behavior.setpeekheight(peekheight); } 

what code above supposed setting bottomsheet peekheight heigth of view. key here function getbottomsheetbehaviour(). implementation below:

private bottomsheetbehavior getbottomsheetbehaviour() {     coordinatorlayout.layoutparams layoutparams = (coordinatorlayout.layoutparams) ((view) getview().getparent()).getlayoutparams();     coordinatorlayout.behavior behavior = layoutparams.getbehavior();     if (behavior != null && behavior instanceof bottomsheetbehavior) {         ((bottomsheetbehavior) behavior).setbottomsheetcallback(mbottomsheetbehaviorcallback);         return (bottomsheetbehavior) behavior;     }     return null; } 

this check if parent of view has 'coordinatorlayout.layoutparams' set. if yes, sets appropriate bottomsheetbehavior.bottomsheetcallback (which needed in next part), , more importantly returns coordinatorlayout.behavior, supposed bottomsheetbehavior.

collapsing:

here [`bottomsheetbehavior.bottomsheetcallback.onslide (view bottomsheet, float slideoffset)``](https://developer.android.com/reference/android/support/design/widget/bottomsheetbehavior.bottomsheetcallback.html#onslide(android.view.view, float)) needed. [documentation](https://developer.android.com/reference/android/support/design/widget/bottomsheetbehavior.bottomsheetcallback.html#onslide(android.view.view, float)):

offset increases bottom sheet moving upward. 0 1 sheet between collapsed , expanded states , -1 0 between hidden , collapsed states.

this means, checking second parameter needed collapse detection:

define bottomsheetbehavior.bottomsheetcallback in same class:

private bottomsheetbehavior.bottomsheetcallback mbottomsheetbehaviorcallback = new bottomsheetbehavior.bottomsheetcallback() {      @override     public void onstatechanged(@nonnull view bottomsheet, int newstate) {         if (newstate == bottomsheetbehavior.state_hidden) {             dismiss();         }     }      @override     public void onslide(@nonnull view bottomsheet, float slideoffset) {         if (slideoffset < 0) {             dismiss();         }     } }; 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -