Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BottomSheetBehavior callback method onSlide() gives confusing slideOffset values

I am implementing an application where I use a bottom sheet with the behavior defined by @string/bottom_sheet_behavior. Additionally I set a BottomSheetBehavior.BottomSheetCallback() programmatically because I want to use the onSlide() method to animate a padding. Therefore I really need the slideOffset value to calculate the new padding to set. I read the docs. There they say that the value ranges from -1 to 0 when the state is between hidden and collapsed state. I only can see this behavior when my phone or tablet is in portrait mode and is independent of the android version. However in landscape mode the range goes all from 1 to -1. In detail here is a value history of a bottom sheet going from hidden to expand:

D/SLIDE: -0.86160713  
D/SLIDE: -0.5714286  
D/SLIDE: -0.29910713  
D/SLIDE: -0.0625  
D/SLIDE: 0.11382114  
D/SLIDE: 0.27235773  
D/SLIDE: 0.40650406  
D/SLIDE: 0.51626015  
D/SLIDE: 0.6300813  
D/SLIDE: 0.69512194  
D/SLIDE: 0.75609756  
D/SLIDE: 0.8130081  
D/SLIDE: 0.8577236  
D/SLIDE: 0.8943089  
D/SLIDE: 0.9186992  
D/SLIDE: 0.9430894  
D/SLIDE: 0.9593496  
D/SLIDE: 0.97154474  
D/SLIDE: 0.9796748  
D/SLIDE: 0.9878049  
D/SLIDE: 0.9918699  
D/SLIDE: 0.99593496  
D/SLIDE: 1.0

I only set the new state of the bottom sheet on a button click. The desired values would begin on -1 and end on 0.0 with more granular steps between. In this history the values increase as well but they go up to +1.
Does anybody else had to face the same strange behavior and can give me a hint what I am missing?
If any additional code is required I will update my question.

Thanks in advance.
Peter

like image 844
PeterG Avatar asked Oct 24 '16 15:10

PeterG


2 Answers

I had the same problem as you, I can not say the problem is fixed because the onSlide method of the BottomSheetBehavior.BottomSheetCallback() is not behaving according to the documentation (values from -1 to 1), but at least I was able to standardize it by setting the peak height to 0, like this:

bottomSheetBehavior.setPeekHeight(0);

After adding this line, I saw that onSlide method always had positive values (0 - 1).

I'm still trying to understand what is happening in order to find a better solution.

like image 107
CynPH Avatar answered Nov 03 '22 08:11

CynPH


BottomSheetBehavior has 3 "fixed" states:

  • STATE_EXPANDED
  • STATE_COLLAPSED
  • STATE_HIDDEN

According to the documentation:

The new offset of this bottom sheet within [-1,1] range. Offset increases as this bottom sheet is moving upward. From 0 to 1 the sheet is between collapsed and expanded states and from -1 to 0 it is between hidden and collapsed states.

You can use setHideable method to exclude/include STATE_HIDDEN.

like image 21
Vasily Galuzin Avatar answered Nov 03 '22 08:11

Vasily Galuzin