BottomSheetDialog 始终获取 Behavour returns null
BottomSheetDialog get Behavour always returns null
我使用 BottomSheetDialog 并且我必须获得 Behavior 所以可以设置 setBottomSheetCallback() 来处理一些东西。
由于google says,我不得不将 Coordinator 放在 parentView 上并向其添加行为。我在 MainActivity (root activity) 中这样定义了 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"
...
这是尝试从 activity:
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"
如您所见,我通过了协调器布局,但方法无法在其中找到行为。
我还应该提到使用 BottonSheetDialog:
的要点
- 我这样显示我的 BottonSheetFragments:
- 我在 OnCreateView(不是在 setupDialog())中对我的 BottomSheetDialog 进行了扩充,以便能够在其中添加 View Pager。如您所知,如果您在 onSetupDialog().
中膨胀视图,ViewPager 将不会附加到 BottonSheetDialog
无论如何我都无法获得父级 CoordinatorLayout 的行为。
在我的 bottonSheetDialog 中,我尝试了这些方法,但没有一个有效,我得到 "The view is not a child of 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;
}
在您的 CoordinatorLayout 中,您 child 具有此 app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
所以你需要BottomSheetBehavior behavior = BottomSheetBehavior.from(your CoordinatorLayout child);
并且您需要在其上设置 setBottomSheetCallback。
behavior.setBottomSheetCallback(...)
BottomSheetDialog
是一个相当特殊的 Dialog
实现。它不会添加到 Activity
布局中的 CoordinatorLayout
,也不依赖于 *。它在内部设置了自己的 CoordinatorLayout
,并在其中设置了 FrameLayout
和 BottomSheetBehavior
,其中放置了您的 View
。 BottomSheetDialog
本身填满了整个屏幕,并具有透明背景,因此它可以处理底部 sheet 交互和任何外部触摸。
如果您需要访问底部 sheet 及其 BottomSheetBehavior
,我们需要从 Dialog
的 View
层次结构中获取它。这就像在 Dialog
上调用 findViewById(R.id.design_bottom_sheet)
一样简单,但我们需要等到显示 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
显示在 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) {}
}
);
}
}
无论哪种情况,您都可以在 BottomSheetCallback
中做任何您想做的事情,只要您 cancel()
在 onStateChanged()
中 Dialog
当 [=40] =].
*顺便说一句,这意味着您不必在 Activity
布局中有 CoordinatorLayout
即可使用 BottomSheetDialog
或 BottomSheetDialogFragment
,但我不确定文档或其他开发人员资源中的任何地方是否明确说明了这一点。
我使用 BottomSheetDialog 并且我必须获得 Behavior 所以可以设置 setBottomSheetCallback() 来处理一些东西。
由于google says,我不得不将 Coordinator 放在 parentView 上并向其添加行为。我在 MainActivity (root activity) 中这样定义了 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"
...
这是尝试从 activity:
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"
如您所见,我通过了协调器布局,但方法无法在其中找到行为。 我还应该提到使用 BottonSheetDialog:
的要点- 我这样显示我的 BottonSheetFragments:
- 我在 OnCreateView(不是在 setupDialog())中对我的 BottomSheetDialog 进行了扩充,以便能够在其中添加 View Pager。如您所知,如果您在 onSetupDialog(). 中膨胀视图,ViewPager 将不会附加到 BottonSheetDialog
无论如何我都无法获得父级 CoordinatorLayout 的行为。 在我的 bottonSheetDialog 中,我尝试了这些方法,但没有一个有效,我得到 "The view is not a child of 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;
}
在您的 CoordinatorLayout 中,您 child 具有此 app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
所以你需要BottomSheetBehavior behavior = BottomSheetBehavior.from(your CoordinatorLayout child);
并且您需要在其上设置 setBottomSheetCallback。
behavior.setBottomSheetCallback(...)
BottomSheetDialog
是一个相当特殊的 Dialog
实现。它不会添加到 Activity
布局中的 CoordinatorLayout
,也不依赖于 *。它在内部设置了自己的 CoordinatorLayout
,并在其中设置了 FrameLayout
和 BottomSheetBehavior
,其中放置了您的 View
。 BottomSheetDialog
本身填满了整个屏幕,并具有透明背景,因此它可以处理底部 sheet 交互和任何外部触摸。
如果您需要访问底部 sheet 及其 BottomSheetBehavior
,我们需要从 Dialog
的 View
层次结构中获取它。这就像在 Dialog
上调用 findViewById(R.id.design_bottom_sheet)
一样简单,但我们需要等到显示 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
显示在 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) {}
}
);
}
}
无论哪种情况,您都可以在 BottomSheetCallback
中做任何您想做的事情,只要您 cancel()
在 onStateChanged()
中 Dialog
当 [=40] =].
*顺便说一句,这意味着您不必在 Activity
布局中有 CoordinatorLayout
即可使用 BottomSheetDialog
或 BottomSheetDialogFragment
,但我不确定文档或其他开发人员资源中的任何地方是否明确说明了这一点。