Show and Hide Bottom Sheet Programmatically

后端 未结 4 674
青春惊慌失措
青春惊慌失措 2020-12-25 10:27

I have implemented Bottom Sheet functionality within my activity in onCreate() using this solution and this library

   sheet = new BottomSheet.Builder(this,          


        
相关标签:
4条回答
  • 2020-12-25 11:10

    To close the BottomSheetDialogFragment from inside the fragment you can call:

    dismiss();
    

    To show or hide the BottomSheetDialogFragment from the activity you can simply call:

    bottomSheetDialogFragment.dismiss();//to hide it
    bottomSheetDialogFragment.show(getSupportFragmentManager(),tag);// to show it
    
    0 讨论(0)
  • 2020-12-25 11:11

    use the following code

    new BottomSheet.Builder(getActivity()).title("Your Title here").sheet(R.menu.bottom_sheet).listener(new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                        case R.id.cancel:
                            dialog.cancel();
                            break;
                        case R.id.view:
                            //Todo view code here
                            dialog.cancel();
                            break;
                    }
                }
            }).show();
    
    0 讨论(0)
  • 2020-12-25 11:16

    For showing the Bottom Sheet use this code:

    bottomSheetInfoBehavior.setHideable(false);
    bottomSheetInfoBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    

    For Hiding the Bottom sheet use this code:

    bottomSheetInfoBehavior.setHideable(true);
    bottomSheetInfoBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
    
    0 讨论(0)
  • 2020-12-25 11:22

    Inside your onClick() of the button use: sheet.show().

    Then when you want to dismiss it, use sheet.dismiss();

    Here below a possible solution:

    BottomSheet sheet = new BottomSheet.Builder(...).build();
    Button button = (Button)findViewById(R.id.mybutton);
    button.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            //you can use isShowing() because BottomSheet inherit from Dialog class
            if (sheet.isShowing()){
                sheet.dismiss();
            } else {
                sheet.show();    
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题