BottomSheetDialogFragment打开一半

时间:2018-08-10 09:04:21

标签: android android-layout bottom-sheet

我的BottomSheetDialogFragment打开时一半打开(意味着不完全打开)。

fragment.show(supportFragmentManager, "my_frag")
  • 我用NestedScrollView尝试了behavior_peekHeight,但是没有用。
  • 尝试不使用NestedScrollView。仅LinearLayout
  • 尝试在match_parentwrap_content之间切换高度

我在RecyclerView布局中有简单的BottomSheetDialogFragment

<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            ...
            >
           <android.support.v7.widget.RecyclerView
           ...
           />

2 个答案:

答案 0 :(得分:14)

BottomSheetFragment表示BottomSheetDialogFragment。要打开支出表,您需要在onCreateDialog()中进行一些更改。

 @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    BottomSheetDialog bottomSheetDialog=(BottomSheetDialog)super.onCreateDialog(savedInstanceState);
    bottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            BottomSheetDialog dialog = (BottomSheetDialog) dialog;
            FrameLayout bottomSheet =  dialog .findViewById(android.support.design.R.id.design_bottom_sheet);
            BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
            BottomSheetBehavior.from(bottomSheet).setSkipCollapsed(true);
            BottomSheetBehavior.from(bottomSheet).setHideable(true);
        }
    });
    return bottomSheetDialog;
}

只需保留布局match_parent,而无需使用NestedScrollView。它对我有用。让我知道您是否仍然遇到问题。

如果有人正在使用新材料库。是
implementation 'com.google.android.material:material:1.0.0'。 然后,您需要更改父级FrameLayout的ID。就是这样。

 @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    BottomSheetDialog bottomSheetDialog=(BottomSheetDialog)super.onCreateDialog(savedInstanceState);
    bottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dia) {
            BottomSheetDialog dialog = (BottomSheetDialog) dia;
            FrameLayout bottomSheet =  dialog .findViewById(com.google.android.material.R.id.design_bottom_sheet);
            BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
            BottomSheetBehavior.from(bottomSheet).setSkipCollapsed(true);
            BottomSheetBehavior.from(bottomSheet).setHideable(true);
        }
    });
    return bottomSheetDialog;
}

在这种情况下,请确保所有从import com.google.android.material的进口。

答案 1 :(得分:1)

您正在访问父视图,因此请使用以下代码将其展开为全屏显示。

View parent = (View) inflatedView.getParent();
parent.setFitsSystemWindows(true);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(parent);
inflatedView.measure(0, 0);
DisplayMetrics displaymetrics = new DisplayMetrics();        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenHeight = displaymetrics.heightPixels;
bottomSheetBehavior.setPeekHeight(screenHeight);

if (params.getBehavior() instanceof BottomSheetBehavior) {
    ((BottomSheetBehavior)params.getBehavior()).setBottomSheetCallback(mBottomSheetBehaviorCallback);
}

params.height = screenHeight;
parent.setLayoutParams(params);

希望它对您有帮助。