问题描述
我正在与 BottomSheetDialog 一起工作,而且我必须获取Behavior,因此可以设置 setBottomSheetCallback()来处理一些事情.
I working with BottomSheetDialog and i have to get Behavior so can set setBottomSheetCallback() to handle some stuff.
如谷歌所说将Coordinator放在parentView上并为其添加行为.我在MainActivity(根活动)中定义了CoordinatorLayout,如下所示:
As google says i had to put Coordinator on parentView and add behavior to it. I defined CoordinatorLayout in MainActivity (root activity) like this:
<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"
...
这是尝试从活动中获得的东西:
This is try to get from 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"
如您所见,我通过了coordinator-layout,但是方法无法在其中找到行为.我还应该提到使用 BottonSheetDialog 的要点:
As you see i passed the coordinator-layout but method can not find behavior in it.I also should mention to points in using BottonSheetDialog:
- 我这样显示我的BottonSheetFragments:
- 我在OnCreateView(不是在setupDialog()中)对 BottomSheetDialog 进行了夸大,以便能够在其中添加View Pager.如您所知,如果您在 onSetupDialog()中对视图进行充气,则ViewPager不会附加到BottonSheetDialog.
- I show my BottonSheetFragments like this:
- I inflated my BottomSheetDialog in OnCreateView (not in setupDialog()) for ability of adding View Pager inside. As you may know ViewPager wont attach to BottonSheetDialog if you inflate view in onSetupDialog().
无论如何,我都无法获得父级CoordinatorLayout的行为.在我的bottonSheetDialog中,我尝试了这些方法,但都无法使用,并且出现视图不是CoordinatorLayout的子视图" 错误.
Any way i could not get behavior of CoordinatorLayout of parent.In my bottonSheetDialog i try these methods and non of them works and i get "The view is not a child of CoordinatorLayout" error.
点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;
}
推荐答案
BottomSheetDialog
是一个非常奇特的Dialog
实现.它不会添加到,也不会依赖Activity
布局中的CoordinatorLayout
.它在内部设置自己的CoordinatorLayout
,并在其中设置带有BottomSheetBehavior
的FrameLayout
,将View
放入其中. BottomSheetDialog
本身充满了整个屏幕,并具有透明的背景,因此它可以处理底部工作表的交互以及任何外部接触.
BottomSheetDialog
is a rather peculiar Dialog
implementation. It is not added to, nor does it rely on, a CoordinatorLayout
in your Activity
's layout. It sets up its own CoordinatorLayout
internally, and within that, a FrameLayout
with BottomSheetBehavior
, into which your View
is placed. The BottomSheetDialog
itself fills the whole screen, and has a transparent background, so that it can handle the bottom sheet interaction, and any outside touches.
如果需要访问该底页及其BottomSheetBehavior
,则需要从Dialog
的View
层次结构中获取它.这就像在Dialog
上调用findViewById(R.id.design_bottom_sheet)
一样简单,但是我们需要等到显示Dialog
来修改BottomSheetBehavior
.此外,由于BottomSheetDialog
设置了自己的BottomSheetCallback
,因此我们必须确保适当替换它.也就是说,当Dialog
达到关闭状态时,我们必须注意取消它.例如:
If you need access to that bottom sheet and its BottomSheetBehavior
, we'll need to get it from the Dialog
's View
hierarchy. That's as simple as calling findViewById(R.id.design_bottom_sheet)
on the Dialog
, but we'll need to wait until the Dialog
is shown to modify the BottomSheetBehavior
. Furthermore, since BottomSheetDialog
sets its own BottomSheetCallback
, we must ensure that we replace it appropriately. That is, we must take care of cancelling the Dialog
when it hits the closed state. For example:
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
调用之后,我们可以覆盖该方法以在此处进行修改.例如:
If you're using a BottomSheetDialogFragment
, the Dialog
is shown in DialogFragment
's onStart()
, and we can override that method to do our modifications there, after the super
call. For example:
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) {}
}
);
}
}
无论哪种情况,只要在newState == BottomSheetBehavior.STATE_HIDDEN
中onStateChanged()
中的cancel()
Dialog
,只要cancel()
中的cancel()
.
In either case, you can do pretty much whatever you want in the BottomSheetCallback
, as long as you cancel()
the Dialog
in onStateChanged()
when newState == BottomSheetBehavior.STATE_HIDDEN
.
这篇关于BottomSheetDialog get Behavour始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!