How can I implement BottomSheetDialogFragment with fixed height

前端 未结 3 2206
难免孤独
难免孤独 2021-02-20 05:31

I need to implement BottomSheetDialogFragment and face with the problem. I need that my BottomSheetDialogFragment has fixed height. Does anyone has an

3条回答
  •  孤街浪徒
    2021-02-20 06:17

    Try following code

    1. Create layout xml file for bottomsheet dialog fragment

    layout_bottom_sheet_dialog_fragment.xml

    
    
    
    
    
        
    
    
            
            
    
    
            
            
    
                
    
                    
    
                    
    
                
    
    
    
                
    
    
            
    
    
    
        
    
        
    
        
    
    
    
    

    1. Create class for bottom sheet fragment that should extends BottomSheetDialogFragment

    BottomSheetFragment.java

    public class BottomSheetFragment extends BottomSheetDialogFragment{
    @BindView(R.id.iv_bottomSheetFrag_closeDialog) AppCompatImageView iv_closeDialog;
    @BindView(R.id.nav_view_bottomSheetFrag_salesPerson) NavigationView nav_view;
    
    
    private Context context;
    
    //public constructor
    public BottomSheetFragment() {
    
    }
    
    //create custom theme for your bottom sheet modal 
    @Override
    public int getTheme() {
        //return super.getTheme();
        return R.style.AppBottomSheetDialogTheme;
    }
    
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //return super.onCreateDialog(savedInstanceState);
        return new BottomSheetDialog(requireContext(), getTheme());  //set your created theme here
    
    }
    
    
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
    
    
    @Override
    public void setupDialog(@NonNull Dialog dialog, int style)
    {
        super.setupDialog(dialog, style);
    
        View contentView = View.inflate(getContext(), R.layout.layout_bottom_sheet_dialog_fragment, null);
        context = contentView.getContext();
        ButterKnife.bind(this, contentView);
        dialog.setContentView(contentView);
        //tv_title.setText(getString(R.string.app_name)); R.style.AppBottomSheetDialogTheme
    
        DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics();
        int width = displayMetrics.widthPixels;
        int height = displayMetrics.heightPixels;
        int maxHeight = (int) (height*0.44); //custom height of bottom sheet
    
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
        CoordinatorLayout.Behavior behavior = params.getBehavior();
        ((BottomSheetBehavior) behavior).setPeekHeight(maxHeight);  //changed default peek height of bottom sheet
    
        if (behavior != null && behavior instanceof BottomSheetBehavior)
        {
            ((BottomSheetBehavior) behavior).setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback()
            {
    
                @Override
                public void onStateChanged(@NonNull View bottomSheet, int newState)
                {
                    String state = "";
                    switch (newState)
                    {
                        case BottomSheetBehavior.STATE_DRAGGING: {
                            //imgBtnClose.setVisibility(View.INVISIBLE);
                            iv_closeDialog.setVisibility(View.GONE);
                            state = "DRAGGING";
                            break;
                        }
                        case BottomSheetBehavior.STATE_SETTLING: {
                            // imgBtnClose.setVisibility(View.INVISIBLE);
                            iv_closeDialog.setVisibility(View.GONE);
                            state = "SETTLING";
                            break;
                        }
                        case BottomSheetBehavior.STATE_EXPANDED: {
                            // imgBtnClose.setVisibility(View.VISIBLE);
                            iv_closeDialog.setVisibility(View.VISIBLE);
                            state = "EXPANDED";
                            break;
                        }
                        case BottomSheetBehavior.STATE_COLLAPSED: {
                            //imgBtnClose.setVisibility(View.INVISIBLE);
                            iv_closeDialog.setVisibility(View.GONE);
                            state = "COLLAPSED";
                            break;
                        }
                        case BottomSheetBehavior.STATE_HIDDEN: {
                            // imgBtnClose.setVisibility(View.INVISIBLE);
                            iv_closeDialog.setVisibility(View.GONE);
                            dismiss();
                            state = "HIDDEN";
                            break;
                        }
                    }
                    Log.i("BottomSheetFrag", "onStateChanged: "+ state);
                }
    
                @Override
                public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                }
            });
        }
    
    
        //close dialog
        iv_closeDialog.setOnClickListener(view -> dismiss());
    
    }
    
    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }}
    
    1. Add these lines in your styles.xml. styles.xml

    
        

    1. Rounded shaped drawable for your bottom sheet. Add this file in your drawables folder.

    rounded_dialog.xml

     
    
        
        
    
    

    1. Finally in your activity call this dialog fragment as follows. Here i have called fragment onClick of bottomNavigationView item onClick listener.

      private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = item ->
    
    
    {
            Fragment selectedFragment = null;
            switch (item.getItemId()) {
                case R.id.bNav_menu:
                    BottomSheetFragment bf = new BottomSheetFragment();
                    bf.show(getSupportFragmentManager(), bf.getTag());
                    //bf.setArguments(bundle);
                    return true;
               
            }
        };

提交回复
热议问题