2017-01-29 60 views
2

我工作BottomSheetDialogFragment.Everything是完美的,但我有一个疑难问题不能在我的BottomSheetDialogFragment.This改变PeekHeight是我的源变化PeekHeight在BottomSheetDialogFragment

public class BottomSheet3DialogFragment extends BottomSheetDialogFragment { 

private BottomSheetBehavior mBottomSheetBehavior2; 
private BottomSheetBehavior.BottomSheetCallback 
     mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() { 
    @Override 
    public void onStateChanged(@NonNull View bottomSheet, int newState) { 
     if (newState == BottomSheetBehavior.STATE_HIDDEN) { 
      dismiss(); 
     } 
     mBottomSheetBehavior2= BottomSheetBehavior.from(bottomSheet); 
     if(mBottomSheetBehavior2!=null) 
      mBottomSheetBehavior2.setPeekHeight(20); 


    } 

    @Override 
    public void onSlide(@NonNull View bottomSheet, float slideOffset) { 
    } 
}; 

@Override 
public void setupDialog(final Dialog dialog, int style) { 
    super.setupDialog(dialog, style); 
    View contentView = View.inflate(getContext(), R.layout.fragment_bottomsheet3, null); 
    dialog.setContentView(contentView); 
} 

}

mButton3.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      BottomSheet3DialogFragment bottomSheetDialogFragment = new BottomSheet3DialogFragment(); 

      bottomSheetDialogFragment.show(getSupportFragmentManager(), bottomSheetDialogFragment.getTag()); 
     } 
    }); 

是否可以在我的片段中更改PeekHeight?如果有人知道 感谢

回答

0

dialog.setContentView(contentView);后的解决方案,请帮助我,你必须把这个:

dialog.setOnShowListener(new OnShowListener() { 
    @Override 
    public void onShow(DialogInterface dialog) { 
    FrameLayout bottomSheet = dialog.getWindow() 
     .findViewById(android.support.design.R.id.design_bottom_sheet); 
    CoordinatorLayout coordinatorLayout = (CoordinatorLayout) bottomSheet.getParent(); 
    BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet); 
    bottomSheetBehavior.setPeekHeight(bottomSheet.getHeight()); 
    coordinatorLayout.getParent().requestLayout(); 
    } 
}); 

该代码会自动调整高度,以查看高度。如果你想要固定高度,只需将bottomSheet.getHeight()更改为任何你想要的。