@@ -153,6 +153,12 @@ public View onCreateDemoView(
153153    persistentBottomSheetBehavior  = BottomSheetBehavior .from (bottomSheetPersistent );
154154    persistentBottomSheetBehavior .addBottomSheetCallback (
155155        createBottomSheetCallback (bottomSheetText ));
156+     bottomSheetPersistent .post (
157+         () -> {
158+           int  state  = persistentBottomSheetBehavior .getState ();
159+           updateStateTextView (bottomSheetPersistent , bottomSheetText , state );
160+           updateBackHandlingEnabled (state );
161+         });
156162    setupBackHandling (persistentBottomSheetBehavior );
157163
158164    Button  button1  = view .findViewById (R .id .cat_bottomsheet_button );
@@ -251,39 +257,39 @@ protected int getStandardBottomSheetLayout() {
251257  }
252258
253259  private  BottomSheetCallback  createBottomSheetCallback (@ NonNull  TextView  text ) {
254-     // Set up  BottomSheetCallback
255-     BottomSheetCallback   bottomSheetCallback  = 
256-          new   BottomSheetCallback ( ) {
257-            @ Override 
258-            public   void   onStateChanged ( @ NonNull   View   bottomSheet ,  int   newState ) { 
260+     return   new   BottomSheetCallback () { 
261+        @ Override 
262+       public   void   onStateChanged ( @ NonNull   View   bottomSheet ,  int   newState ) {
263+         updateStateTextView ( bottomSheet ,  text ,  newState ); 
264+       } 
259265
260-             switch  (newState ) {
261-               case  BottomSheetBehavior .STATE_DRAGGING :
262-                 text .setText (R .string .cat_bottomsheet_state_dragging );
263-                 break ;
264-               case  BottomSheetBehavior .STATE_EXPANDED :
265-                 text .setText (R .string .cat_bottomsheet_state_expanded );
266-                 break ;
267-               case  BottomSheetBehavior .STATE_COLLAPSED :
268-                 text .setText (R .string .cat_bottomsheet_state_collapsed );
269-                 break ;
270-               case  BottomSheetBehavior .STATE_HALF_EXPANDED :
271-                 BottomSheetBehavior <View > bottomSheetBehavior  =
272-                     BottomSheetBehavior .from (bottomSheet );
273-                 text .setText (
274-                     getString (
275-                         R .string .cat_bottomsheet_state_half_expanded ,
276-                         bottomSheetBehavior .getHalfExpandedRatio ()));
277-                 break ;
278-               default :
279-                 break ;
280-             }
281-           }
266+       @ Override 
267+       public  void  onSlide (@ NonNull  View  bottomSheet , float  slideOffset ) {}
268+     };
269+   }
282270
283-           @ Override 
284-           public  void  onSlide (@ NonNull  View  bottomSheet , float  slideOffset ) {}
285-         };
286-     return  bottomSheetCallback ;
271+   private  void  updateStateTextView (@ NonNull  View  bottomSheet , @ NonNull  TextView  text , int  state ) {
272+     switch  (state ) {
273+       case  BottomSheetBehavior .STATE_DRAGGING :
274+         text .setText (R .string .cat_bottomsheet_state_dragging );
275+         break ;
276+       case  BottomSheetBehavior .STATE_EXPANDED :
277+         text .setText (R .string .cat_bottomsheet_state_expanded );
278+         break ;
279+       case  BottomSheetBehavior .STATE_COLLAPSED :
280+         text .setText (R .string .cat_bottomsheet_state_collapsed );
281+         break ;
282+       case  BottomSheetBehavior .STATE_HALF_EXPANDED :
283+         BottomSheetBehavior <View > bottomSheetBehavior  =
284+             BottomSheetBehavior .from (bottomSheet );
285+         text .setText (
286+             getString (
287+                 R .string .cat_bottomsheet_state_half_expanded ,
288+                 bottomSheetBehavior .getHalfExpandedRatio ()));
289+         break ;
290+       default :
291+         break ;
292+     }
287293  }
288294
289295  private  void  setupBackHandling (BottomSheetBehavior <View > behavior ) {
@@ -294,25 +300,29 @@ private void setupBackHandling(BottomSheetBehavior<View> behavior) {
294300        new  BottomSheetCallback () {
295301          @ Override 
296302          public  void  onStateChanged (@ NonNull  View  bottomSheet , int  newState ) {
297-             switch  (newState ) {
298-               case  BottomSheetBehavior .STATE_EXPANDED :
299-               case  BottomSheetBehavior .STATE_HALF_EXPANDED :
300-                 persistentBottomSheetBackCallback .setEnabled (true );
301-                 break ;
302-               case  BottomSheetBehavior .STATE_COLLAPSED :
303-               case  BottomSheetBehavior .STATE_HIDDEN :
304-                 persistentBottomSheetBackCallback .setEnabled (false );
305-                 break ;
306-               case  BottomSheetBehavior .STATE_DRAGGING :
307-               case  BottomSheetBehavior .STATE_SETTLING :
308-               default :
309-                 // Do nothing, only change callback enabled for "stable" states. 
310-                 break ;
311-             }
303+             updateBackHandlingEnabled (newState );
312304          }
313305
314306          @ Override 
315307          public  void  onSlide (@ NonNull  View  bottomSheet , float  slideOffset ) {}
316308        });
317309  }
310+ 
311+   private  void  updateBackHandlingEnabled (int  state ) {
312+     switch  (state ) {
313+       case  BottomSheetBehavior .STATE_EXPANDED :
314+       case  BottomSheetBehavior .STATE_HALF_EXPANDED :
315+         persistentBottomSheetBackCallback .setEnabled (true );
316+         break ;
317+       case  BottomSheetBehavior .STATE_COLLAPSED :
318+       case  BottomSheetBehavior .STATE_HIDDEN :
319+         persistentBottomSheetBackCallback .setEnabled (false );
320+         break ;
321+       case  BottomSheetBehavior .STATE_DRAGGING :
322+       case  BottomSheetBehavior .STATE_SETTLING :
323+       default :
324+         // Do nothing, only change callback enabled for "stable" states. 
325+         break ;
326+     }
327+   }
318328}
0 commit comments