状态改变后的Android底部表

时间:2017-02-17 15:38:24

标签: android bottom-sheet

我有一个带有底片的屏幕,但是为了让活动之间的过渡动​​画起作用,我需要在用户按下时将底部折叠。我试过这个

@Override
public void onBackPressed(){
    if (mBottomSheetBehavior.getState()==BottomSheetBehavior.STATE_COLLAPSED){
        super.onBackPressed();
    }else{
        mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        super.onBackPressed();
    }
    mShowingBack = false;
}

然而,由于活动回归而底部表格仅下降一半,因此无效。

2 个答案:

答案 0 :(得分:4)

BottomSheetBehavior.STATE_COLLAPSED并不隐藏所有BottomSheet,它只是将视图的高度设置为你在xml :)中使用setPeekHeight()或behavior_peekHeight设置的任何内容,但是把它放在一边......你应该调用super当BottomSheet的状态为STATE_COLLAPSED时,BottomSheetBehaviorCallback中的.onBackPressed(),如下所示:

BottomSheetBehavior behavior = BottomSheetBehavior.from(mBottomSheetBehavior);  
behavior.setBottomSheetCallback(new BottomSheetCallback() {  
   @Override  
   public void onStateChanged(@NonNull View bottomSheet, int newState) {  
     if (newState == BottomSheetBehavior.STATE_COLLAPSED && mIsCollapsedFromBackPress){
        mIsCollapsedFromBackPress = false;
        super.onBackPressed();
     }
   }  

  @Override  
  public void onSlide(@NonNull View bottomSheet, float slideOffset) {  
     // React to dragging events  
  }  
});

并且你的backPressed()方法应如下所示:

@Override
public void onBackPressed(){
    mIsCollapsedFromBackPress = true;
    mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}

答案 1 :(得分:2)

behavior.setBottomSheetCallback(); 

现在已弃用。改用这个。

BottomSheetBehavior.from(nearbyBottomSheet).addBottomSheetCallback(object :BottomSheetBehavior.BottomSheetCallback() {
        override fun onStateChanged(bottomSheet: View, newState: Int) {

        }

        override fun onSlide(bottomSheet: View, slideOffset: Float) {
                
        }
})