如何更改BottomSheetDialogFragment的BottomSheetBehavior?

4
我有一个 modal BottomSheetDialog。当前和默认的行为是,当我将对话框向下拖动一半时,它会关闭。例如,假设对话框值从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_UP没有被调用。有任何想法吗?谢谢。

2个回答

4
如果您想修改底部对话框片段中底部对话框的行为,请使用以下代码:
@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)
            }
        }
      })
    }

这将帮助您设置展开和可隐藏选项,您还可以设置自定义的展开大小。

2

我认为您可以自定义属性behavior_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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接