2016-05-13 56 views
1

我知道如何change the height of a Bottom Sheet。 增加底部紙張的高度沒有問題。不過,我不能用下面的代碼來降低它的高度。減少底板的高度

bottomSheetBehavior.setPeekHeight(peekHeight); // peekHeight < previous height 
bottomSheetBehavior.setState(STATE_COLLAPSED); 

任何人都遇到同樣的問題?

+0

因此,如果您在設置一次後更改您的窺視高度,它不會被應用? –

+0

在設置peekHeight之前,您是否嘗試過使用BottomSheetBehavior.from(bottomSheet)?你可以傳遞你的bottomSheet作爲參數,然後添加上面使用的行 –

回答

3

我想要做同樣的事情,我沒有這樣的問題。我可以輕鬆地增加和減少我的身高BottomSheetDialogFragment。這裏是我的片段的兩種方法的代碼:

private BottomSheetBehavior.BottomSheetCallback bottomSheetCallback = 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) { 
     } 
    }; 

    public static BottomSheetFragment newInstance() { 
     return new BottomSheetFragment(); 
    } 

    @Override 
    public void setupDialog(Dialog dialog, int style) { 
     super.setupDialog(dialog, style); 
     View contentView = View.inflate(getContext(), R.layout.bottom_sheet_dialog_content_view, null); 
     dialog.setContentView(contentView); 
     CoordinatorLayout.LayoutParams layoutParams = ((CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams()); 
     CoordinatorLayout.Behavior behavior = layoutParams.getBehavior(); 
     if (behavior != null && behavior instanceof BottomSheetBehavior) { 
      ((BottomSheetBehavior) behavior).setBottomSheetCallback(bottomSheetCallback); 
      ((BottomSheetBehavior) behavior).setPeekHeight(getResources().getDimensionPixelSize(R.dimen.bottom_sheet_height)/4); 
     } 

     initRecyclerView(contentView); 
    }