BottomSheetDialogFragment

BottomSheetDialogFragment

This question already has answers here:
Set state of BottomSheetDialogFragment to expanded

(15个回答)


2年前关闭。




当我打开BottomSheetDialogFragment时,它打开一半(表示不完全打开)。
fragment.show(supportFragmentManager, "my_frag")
  • 我尝试了NestedScrollViewbehavior_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
               ...
               />
    

    最佳答案

    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。它对我有用。如果您仍然遇到问题,请告诉我。

    如果有人正在使用“新 Material ”库。哪一个implementation 'com.google.android.material:material:1.0.0'
    然后,您需要更改Parent的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导入。

    10-06 03:25