BottomSheetDialog get Behavour 总是返回 null

Ken*_*nji 3 android behavior android-coordinatorlayout

我使用BottomSheetDialog并且我必须获得 Behavior 以便可以设置setBottomSheetCallback()来处理一些东西。

正如谷歌所说,我必须将 Coordinator 放在 parentView 上并向其添加行为。我在 MainActivity(根活动)中定义了 CoordinatorLayout,如下所示:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:tag="coordinatorLayout"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"

...
Run Code Online (Sandbox Code Playgroud)

这是尝试从活动中获取:

 public void setupDialog(final Dialog dialog, int style) {

 CoordinatorLayout coordinatorLayout = getActivity().getWindow().getDecorView();
 BottomSheetBehavior behavior = BottomSheetBehavior.from(coordinatorLayout);
Run Code Online (Sandbox Code Playgroud)

我也试过:

CoordinatorLayout coordinatorLayout = getActivity().getWindow().getDecorView().findViewById(R.id.coordinatorLayout); 
//this is point to the coordinatorView 

BottomSheetBehavior behavior = BottomSheetBehavior.from(coordinatorLayout);
//But this returns same error that "The view is not a child of CoordinatorLayout"
Run Code Online (Sandbox Code Playgroud)

如您所见,我通过了协调器布局,但方法无法在其中找到行为。我还应该提到使用BottonSheetDialog 的要点

  1. 我像这样显示我的 BottonSheetFragments:
  2. 我在 OnCreateView(而不是 setupDialog())中膨胀了我的BottomSheetDialog,以便在里面添加 View Pager。如您所知,如果您在onSetupDialog() 中膨胀视图,则 ViewPager 不会附加到 BottonSheetDialog 。

无论如何我都无法获得父级 CoordinatorLayout 的行为。在我的 bottonSheetDialog 中,我尝试了这些方法,但它们都不起作用,并且出现“视图不是 CoordinatorLayout 的子项” 错误。

第 1 点的代码:

MyFragment myFragment= MyFragment.getInstance(bundle);
myFragment.show(fragment.getChildFragmentManager(),"tag");
Run Code Online (Sandbox Code Playgroud)

第 2 点的代码:

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_bottomsheet, null, false);  
return rootView;
}
Run Code Online (Sandbox Code Playgroud)

Mik*_* M. 7

BottomSheetDialog是一个相当奇特的Dialog实现。它不添加,也不依赖于*,一个CoordinatorLayout在你Activity的布局。它在CoordinatorLayout内部建立了自己的,并在其中建立了一个FrameLayoutwith BottomSheetBehavior,其中View放置了您的。它BottomSheetDialog本身填满了整个屏幕,并具有透明背景,因此它可以处理底部表单交互和任何外部触摸。

如果您需要访问该底部工作表及其BottomSheetBehavior,我们需要从DialogView层次结构中获取它。这是一样简单调用findViewById(R.id.design_bottom_sheet)Dialog,但我们需要等到Dialog显示修改BottomSheetBehavior。此外,由于BottomSheetDialog设置了自己的BottomSheetCallback,我们必须确保我们适当地替换它。也就是说,Dialog当它达到关闭状态时,我们必须注意取消它。例如:

final BottomSheetDialog bsd = new BottomSheetDialog(MainActivity.this);
bsd.setContentView(R.layout.your_dialog_layout);
bsd.show();

FrameLayout bottomSheet = (FrameLayout) bsd.findViewById(R.id.design_bottom_sheet);
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(View bottomSheet, int newState) {
            // This is the crucial bit.
            if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                bsd.cancel();
            }
        }

        @Override
        public void onSlide(View bottomSheet, float slideOffset) {}
    }
);
Run Code Online (Sandbox Code Playgroud)

如果您使用的是BottomSheetDialogFragmentDialog则显示在DialogFragment的 中onStart(),我们可以在super调用后覆盖该方法以在那里进行修改。例如:

public class MyFragment extends BottomSheetDialogFragment {
    public MyFragment() {}

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

    @Override
    public void onStart() {
        super.onStart();

        FrameLayout bottomSheet = getDialog().findViewById(R.id.design_bottom_sheet);
        BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
        behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                @Override
                public void onStateChanged(View bottomSheet, int newState) {
                    // This is the crucial bit.
                    if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                        getDialog().cancel();
                    }
                }

                @Override
                public void onSlide(View bottomSheet, float slideOffset) {}
            }
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,你可以做很多你想要的一切BottomSheetCallback,你只要cancel()DialogonStateChanged()的时候newState == BottomSheetBehavior.STATE_HIDDEN


*顺便说一下,这意味着您不必CoordinatorLayout在您的Activity'a 布局中使用BottomSheetDialogor BottomSheetDialogFragment,尽管我不确定文档或其他开发人员资源中的任何地方是否明确说明了这一点。

  • 完整且有用。 (2认同)