首页 > 解决方案 > BottomSheetDialogFragment 打开一半

问题描述

BottomSheetDialogFragment当我打开它时,我打开了一半(意思是没有完全打开)。

fragment.show(supportFragmentManager, "my_frag")

RecyclerViewBottomSheetDialogFragment布局很简单。

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

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

标签: androidandroid-layoutbottom-sheet

解决方案


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'。然后你需要改变 Parent 的 id FrameLayout。所以会的。

 @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在这种情况下,请确保您的所有导入。


推荐阅读