Java 如何更改底部图纸对话框行为

Java 如何更改底部图纸对话框行为,java,android,kotlin,bottom-sheet,android-bottomsheetdialog,Java,Android,Kotlin,Bottom Sheet,Android Bottomsheetdialog,我有一个模态的底部表单对话框。对话框的当前和默认行为是,当我将对话框向下拖动一半时,它将关闭。例如,假设对话框值从0.0(折叠)到1.0(展开)。所以当用户将其拖动到0.5时,它将崩溃。但我想要的行为是,当我将对话框向下拖动到0.8并取下手指时,关闭对话框。我怎样才能做到这一点,有没有办法 此外,我认为如果只在我使用任何拖动按钮拖动对话框时才允许关闭该对话框(大多数情况下,它是一个简单的ImageView) 所以我想要的是,当用户将对话框向下移动(拖动)一点时,关闭对话框 所以我现在的代码是:

我有一个模态的
底部表单对话框
。对话框的当前和默认行为是,当我将对话框向下拖动一半时,它将关闭。例如,假设对话框值从0.0(折叠)到1.0(展开)。所以当用户将其拖动到0.5时,它将崩溃。但我想要的行为是,当我将对话框向下拖动到0.8并取下手指时,关闭对话框。我怎样才能做到这一点,有没有办法

此外,我认为如果只在我使用任何拖动按钮拖动对话框时才允许关闭该对话框(大多数情况下,它是一个简单的
ImageView

所以我想要的是,当用户将对话框向下移动(拖动)一点时,关闭对话框

所以我现在的代码是:

public class FiltersBottomSheet extends BottomSheetDialogFragment {

private FragmentFilterBinding binding;

public FiltersBottomSheet() {}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    binding = DataBindingUtil.inflate(inflater, R.layout.fragment_filter, container, false);
    View view = binding.getRoot();

    // I'm using data binding
    // There is a little logic, but now you don't need it :)

    return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    view.getViewTreeObserver()
            .addOnGlobalLayoutListener(() -> {
                BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
                LinearLayout bottomSheet = dialog.findViewById(R.id.bottom_frame);

                if (bottomSheet != null) {
                    BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
                    behavior.setState(BottomSheetBehavior.STATE_EXPANDED);

                    behavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                        @Override
                        public void onStateChanged(@NonNull View bottomSheet, int newState) {
                            //Log.d("Tag___1", "NewState: " + newState);
                        }

                        @Override
                        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                            //Log.d("Tag___1", "Offset: " + slideOffset);
                        }
                    });
                }
            });
}
}

所以首先我认为,
onSlide()
可以帮助我,但是。。。此方法仅在用户展开或折叠对话框时调用。例如,当用户触摸对话框并开始向下拖动时,onSlide()方法不会被调用。您也可以看到上面我调用了
view.getViewTreeObserver()
,我尝试将
onTouchListener
添加到此视图中。问题是,当用户在移动对话框后移开手指时,
MotionEvenet.ACTION\u UP
没有调用。有什么想法吗?谢谢。

如果要修改底部对话框片段中的底部工作表对话框行为,请使用以下命令:

@Override
    public void onViewCreated(NotNull@ View view, Nullable@ Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState)
      view.getViewTreeObserver()
                .addOnGlobalLayoutListener(() -> {
        BottomSheetDialog dialog =(BottomSheetDialog) getDialog ()
      if (dialog != null) {
            FrameLayout bottomSheet = dialog.findViewById (R.id.design_bottom_sheet)
          if (bottomSheet != null) {
                BottomSheetBehavior behavior = BottomSheetBehavior.from (bottomSheet)
              behavior.setState(BottomSheetBehavior.STATE_EXPANDED)
              behavior.setSkipCollapsed(true)
              behavior.setHideable(false)
            }
        }
      })
    }

这将有助于您声明expanded and Hiddeble false,您也可以将自定义大小设置为expanded。

我认为您可以自定义属性
行为\u halfExpandedRatio
,也可以自定义滑动时的回调:

val bottomSheetCallback = object : BottomSheetBehavior.BottomSheetCallback() {

    override fun onStateChanged(bottomSheet: View, newState: Int) {
        // Do something for new state
    }

    override fun onSlide(bottomSheet: View, slideOffset: Float) {
        // Do something for slide offset
    }
}
bottomSheetBehavior.addBottomSheetCallback(bottomSheetCallback)