Android 以编程方式显示和隐藏底部工作表

Android 以编程方式显示和隐藏底部工作表,android,material-design,bottom-sheet,Android,Material Design,Bottom Sheet,我已经使用解决方案和库在onCreate()中的活动中实现了底部工作表功能 sheet=new BottomSheet.Builder(此R.style.BottomSheet_对话框) .名称(“新”) .grid()/在按钮的onClick()内使用:sheet.show() 然后,当您想解除它时,使用sheet.disclose() 下面是一个可能的解决方案: BottomSheet sheet = new BottomSheet.Builder(...).build(); Button

我已经使用解决方案和库在onCreate()中的活动中实现了底部工作表功能

sheet=new BottomSheet.Builder(此R.style.BottomSheet_对话框)
.名称(“新”)
.grid()/在按钮的
onClick()
内使用:
sheet.show()

然后,当您想解除它时,使用
sheet.disclose()

下面是一个可能的解决方案:

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();    
        }
    }
});
使用以下代码

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();

要从片段内部关闭BottomSheetDialogFragment,可以调用:

dismiss();
要在活动中显示或隐藏BottomSheetDialogFragment,只需调用:

bottomSheetDialogFragment.dismiss();//to hide it
bottomSheetDialogFragment.show(getSupportFragmentManager(),tag);// to show it

要显示底部图纸,请使用以下代码:

bottomSheetInfoBehavior.sethidable(false);
bottomSheetInfoBehavior.setState(BottomSheetBehavior.STATE_展开);
要隐藏底部图纸,请使用以下代码:

bottomSheetInfoBehavior.sethidable(true);
bottomSheetInfoBehavior.setState(BottomSheetBehavior.STATE_隐藏);

如果要在片段中隐藏,请使用

this.dismiss();


您好,sista,我想您可以根据视图是否可见,通过sheet.show()和sheet.dislose()来实现。我使用
bottomSheetBehavior.setState(bottomSheetBehavior.STATE_EXPANDED)
来显示它。
this.dismiss();
YOUR_FRAGMENT.this.dismiss()