Android – view is at the last position of the parent, but it is still blocked

What I want to do

In the bottom sheet dialog fragment, I want a view that always sticks to the bottom of the screen, regardless of whether the bottom sheet behavior is in state (collapsed / expanded)

What did I do?

In the subclass of bottomsheetdialogfragment, I extended a view from XML and added it as the child node of coordinatorlayout (this is the parent node of the parent node of 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);
}

The code runs without error. When I use the layout inspector to view the view hierarchy, the view structure is also correct:

You can also download the layout checker result here and open it using your own Android studio

problem

However, even if it is inserted as the last sub item of coordinatorlayout, it is still blocked by bottomsheetdialogfragment. When I slowly scroll down bottomsheetdialogframemnt (from folded state to hidden state), I can finally see the view I want to expand behind the fragment

Why is that?

answer

As @ gooddev correctly pointed out, this is because the root view (design_bottom_sheet) has been set to Z transformation by bottomsheetdialog. This provides an important information - not only the sequence in the view hierarchy will determine its visibility, but also its z transformation

The best way is to get design_ bottom_ The Z value of sheet and set it as the bottom bar setting

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

resolvent:

Edit 2

OK, now I see your request. Try this:

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

Edit:

OK, I'll check your layout and check the source code of bottomsheetdialogfragment to find the reason:

Use the bottomsheetdialog dialog box in the bottomsheetdialogfragment, and the setcontentview method. Use wrapinbottomsheet in the bottomsheetdialog to place the content view in r.id.design_ bottom_ Sheet layout. Therefore, you need to overwrite the public view oncreateview (layouteinflator, ViewGroup container, bundle savedinstancestate) of bottomsheetdialogfragment to fix your problem

Alternatively, change the setupbottombar method to:

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

And in your item_ selection_ In the bar layout file, change the height and 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 doc says: modal bottom page. This is a version of dialogfragment. It uses bottomsheetdialog instead of floating dialog to display the bottom worksheet

Therefore, the bottomsheetdialogfragment is a dialog, and the dialog is a floating view. Therefore, when the bottomsheetdialogfragment is displayed, the activity content will be overwritten

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>