BottomSheetDialog get Behavour始终返回null

时间:2018-11-18 12:01:57

标签: android android-coordinatorlayout behavior

我正在与 BottomSheetDialog 一起工作,而且我必须获取Behavior,因此可以设置 setBottomSheetCallback()来处理一些事情。

作为google says,我不得不将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"

...

这是从活动中获得的尝试:

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

 CoordinatorLayout coordinatorLayout = getActivity().getWindow().getDecorView();
 BottomSheetBehavior behavior = BottomSheetBehavior.from(coordinatorLayout);

我也尝试过:

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"

如您所见,我通过了coordinator-layout,但是方法无法在其中找到行为。 我还应该提到使用 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");

第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;
}

2 个答案:

答案 0 :(得分:1)

BottomSheetDialog是一个非常特殊的Dialog实现。它不会添加到您的CoordinatorLayout布局中的Activity上,也不会依赖于它。它在内部设置自己的CoordinatorLayout,并在其中设置FrameLayoutBottomSheetBehavior,将View放入其中。 BottomSheetDialog本身充满了整个屏幕,并具有透明的背景,因此它可以处理底部工作表的交互以及任何外部接触。

如果您需要访问该底部工作表及其BottomSheetBehavior,则需要从Dialog的{​​{1}}层次结构中获取它。这就像在View上调用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) {} } ); ,则BottomSheetDialogFragment会显示在Dialog的{​​{1}}中,我们可以覆盖该方法以在此位置进行修改DialogFragment调用。例如:

onStart()

无论哪种情况,只要superpublic 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) {} } ); } } BottomSheetCallback中的cancel()Dialog中的任何事情都可以做


*偶然地,这意味着您不必onStateChanged()的布局中有newState == BottomSheetBehavior.STATE_HIDDEN即可使用CoordinatorLayoutActivity,尽管我'不确定文档或其他开发人员资源中的任何地方都清楚了。

答案 1 :(得分:0)

在您的CoordinatorLayout中,您的孩子有这个<form id="submit-button"> <label for="name">Serial number:</label> <input type="text" name="form_serialno" autofocus="autofocus" placeholder="Serial nomber"/> <input type="hidden" name="form_idorder" value="<?PHP echo $idorder; ?>"> <input type="hidden" name="form_customer" value="<?PHP echo $Customer; ?>"> <input type="submit" value="submit" /> </form>

所以您需要制作app:layout_behavior="android.support.design.widget.BottomSheetBehavior"

在其上,您需要进行setBottomSheetCallback。 BottomSheetBehavior behavior = BottomSheetBehavior.from(your CoordinatorLayout child);

相关问题