gpt4 book ai didi

android - BottomSheetDialog get Behavour 总是返回 null

转载 作者:搜寻专家 更新时间:2023-11-01 07:42:25 24 4
gpt4 key购买 nike

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

作为google says我不得不将 Coordinator 放在 parentView 上并向其添加行为。我在 MainActivity(根 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 的要点:

  1. 我这样显示我的 BottonSheetFragments:
  2. 我在 OnCreateView(不是在 setupDialog() 中)中膨胀了我的 BottomSheetDialog,以便能够在其中添加 View Pager。如您所知,如果您在 onSetupDialog() 中膨胀 View ,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;
}

最佳答案

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

如果您需要访问该底部工作表及其 BottomSheetBehavior,我们需要从 DialogView 层次结构中获取它.这就像在 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 会显示在 DialogFragmentonStart() 中,我们可以覆盖该方法,在 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() Dialog onStateChanged()newState == BottomSheetBehavior.STATE_HIDDEN 时。


*顺便说一下,这意味着您不必在Activity布局中有一个CoordinatorLayout 来使用BottomSheetDialogBottomSheetDialogFragment,但我不确定文档或其他开发人员资源中是否明确说明了这一点。

关于android - BottomSheetDialog get Behavour 总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53360648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com