Android studio展开底部表单按钮点击进入其片段

brjng4g3  于 2022-11-27  发布在  Android
关注(0)|答案(1)|浏览(134)

我有一个底部工作表,我想添加一个按钮,单击该按钮时底部工作表对话框将展开
enter image description here
是否可以将onclicklistener放在底部表单片段onCreateView中并更改对话框的状态?
这是我的底片碎片

public class RepliesToComment extends BottomSheetDialogFragment {

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
        Window window = dialog.getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        dialog.getBehavior().setState(BottomSheetBehavior.STATE_COLLAPSED); <--change to STATE_EXPANDED on click-->

        return dialog;
    }

ImageButton expandBtn;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_replies_to_comment, container, false);
    {...}

    expandBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ----   
        }
    });
}
ccrfmcuu

ccrfmcuu1#

我设法使它工作这是我的解决方案
然后创建一个布尔标识符来切换对话框的状态

boolean identifier = true;
BottomSheetDialog dialog;

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
    Window window = dialog.getWindow();
    window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

    return dialog;
}

public void expand(){
    if (!identifier){
        dialog.getBehavior().setState(BottomSheetBehavior.STATE_COLLAPSED);
    }else {
        dialog.getBehavior().setState(BottomSheetBehavior.STATE_EXPANDED);
    }
}

然后把按钮做成切换按钮来改变标识符的值

//expand
    expandBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            identifier = b;
            expand();
        }
    });

相关问题