view在parent的最后一个位置,但是还是被挡住了

View is in the last position of parent, but still being blocked

我想做什么

BottomSheetDialogFragment 中,我想膨胀一个始终停留在屏幕底部的视图,无论 state (collapsed / expanded) BottomSheetBehavior 在什么地方。

我做了什么

BottomSheetDialogFragment 的子类中,我膨胀了 XML 的一个视图并将其添加为 CoordinatorLayout 的子类(BottomSheetDialogFragment 的父类的父类) :

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setupBottomBar(getView());
}

private void setupBottomBar (View rootView) {
    CoordinatorLayout parentView = (CoordinatorLayout) ((FrameLayout)rootView.getParent()).getParent();
    parentView.addView(LayoutInflater.from(getContext()).inflate(R.layout.item_selection_bar, parentView, false), -1);
}

代码运行没有错误。
而当我使用Layout Inspector查看View层级时,视图结构也是正确的:

您还可以下载布局检查器结果 here,并使用您自己的 Android Studio 打开它。

问题

但是,即使它作为 CoordinatorLayout 的最后一个子项插入,它仍然被 BottomSheetDialogFragment 阻塞。
当我慢慢向下滚动 BottomSheetDialogFragemnt(从折叠状态到隐藏状态)时,我终于可以看到片段后面要膨胀的视图。

为什么会这样?

答案

正如@GoodDev 正确指出的那样,这是因为根视图 (design_bottom_sheet) 已被 BottomSheetDialog.
设置为 Z 平移 这提供了一个重要信息,即 - 不仅视图层次结构中的顺序将决定其可见性,而且其 Z 平移也将决定。

最好的办法是获取design_bottom_sheet的Z值,设置为底栏布局

private void setupBottomBar (View rootView) {
    CoordinatorLayout parentView = (CoordinatorLayout) (rootView.getParent().getParent());
    View barView = LayoutInflater.from(getContext()).inflate(R.layout.item_selection_bar, parentView, false);
    ViewCompat.setTranslationZ(barView, ViewCompat.getZ((View)rootView.getParent()));
    parentView.addView(barView, -1);
}

编辑 2

好的,现在我看到你的要求了,试试这个:

private void setupBottomBar (View rootView) {
    CoordinatorLayout parentView = (CoordinatorLayout) ((FrameLayout)rootView.getParent()).getParent();
    View view = LayoutInflater.from(getContext()).inflate(R.layout.item_selection_bar, parentView, false);
    // using TranslationZ to put the view on top of bottom sheet layout
    view.setTranslationZ(100);
    parentView.addView(view, -1);
}

编辑:

好的,我检查了你的布局并检查了BottomSheetDialogFragment源代码,找到了原因:

BottomSheetDialogFragment中使用BottomSheetDialog对话框,BottomSheetDialog using wrapInBottomSheet中的方法setContentView将内容视图置于R.id.design_bottom_sheet布局中。所以你需要覆盖 BottomSheetDialogFragmentpublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 来解决你的问题。

或者,将您的 setupBottomBar 方法更改为:

private void setupBottomBar (View rootView) {
    FrameLayout frame = (FrameLayout)rootView.getParent();
    frame.addView(LayoutInflater.from(getContext()).inflate(R.layout.item_selection_bar, frame, false), -1);
}

并在您的 item_selection_bar 布局文件中,更改高度和 layout_gravity:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_gravity="bottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

BottomSheetDialogFragment 文档说:模态底部 sheet。这是一个使用 BottomSheetDialog 而不是浮动对话框显示底部 sheet 的 DialogFragment 版本。

因此 BottomSheetDialogFragmentDialogDialog 是浮动视图,因此 BottomSheetDialogFragment 显示时会覆盖 Activity 内容。

@goodev 给出了一个很好的答案。

你的问题

View的Z位置导致了这个问题。虽然TextView是最后一个位置你还是看不到它。

如何解决

您可以将 design_sheet_bottom's Z 设置为 TextView。

 private void setupBottomBar (View rootView) {
    CoordinatorLayout parentView = (CoordinatorLayout) ((FrameLayout)rootView.getParent()).getParent();
    View view = LayoutInflater.from(getContext()).inflate(R.layout.item_selection_bar, parentView, false);
    view.setZ(((View)rootView.getParent()).getZ());
    parentView.addView(view, -1);
}

而且我觉得上面的方式很无聊,你能把你的两个视图 RecyclerViewTextView 放到一个布局中吗?然后你可以在 onCreateView() 方法中一起膨胀主题。