底部的隐藏状态 Sheet

Hidden state of the Bottom Sheet

我正在尝试为 BottomSheet 设置隐藏状态,但它不起作用。有什么问题吗?

 bottomBar = BottomSheetBehavior.from(findViewById(R.id.bottom_bar));
 bottomBar.setState(BottomSheetBehavior.STATE_HIDDEN);

尝试BottomSheetBehaviour.STATE_COLLAPSED

bottomBar = BottomSheetBehavior.from(findViewById(R.id.bottom_bar));
bottomBar.setState(BottomSheetBehavior.STATE_COLLAPSED);

确保您在 Activity 的生命周期中没有过早地这样做。如果你需要在 onCreate 或类似的地方做,试着把它放在你 post 的 Runnable 视图中,像这样:

getWindow().getDecorView().post(new Runnable() {
    @Override
    public void run() {
        bottomBar = BottomSheetBehavior.from(findViewById(R.id.bottom_bar));
        bottomBar.setState(BottomSheetBehavior.STATE_HIDDEN);
    }
 });

这不是最干净的解决方案,但有时不可避免。

尝试以下操作:

LinearLayout bottomSheetViewgroup  
= (LinearLayout) findViewById(R.id.bottom_sheet);

BottomSheetBehavior bottomSheetBehavior =  
BottomSheetBehavior.from(bottomSheetViewgroup);

然后使用

bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);

记得在 activity/fragment

开头隐藏底部 sheet 时添加此内容
bottomSheetBehavior =BottomSheetBehavior.from(bottom_sheet_view_here);
bottomSheetBehavior.setHideable(true);//Important to add
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
mBottomSheetBehaviour.setPeekHeight(0);

使用这个它会隐藏。

如果您使用选项卡 Activity 之类的东西,您可以在 Fragment 中隐藏 bottomsheetlayout。

我认为这是可能的,因为片段视图是在 activity 视图之后创建的。

class "activity"
   public void hideBottomSheet(){ 
      sheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
   }

class "fragment"
   onCreateView()
      ((YourActivity.class)getActivity()).hideBottomSheet();

另一种方式 - 这样你就不需要 Fragments:

boolean init = true;


layoutBottomSheet.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
            if(init)hideBottomSheet();
            init=false;
        }
    });
 bottomsheetbehavior.setPeekHeight(0, true);

用小动画隐藏:)

            bottomSheet.animate()
                    .translationYBy(bottomSheetBehavior.getPeekHeight());