简体   繁体   中英

How to pop up whole fragment layout with Android snackbar

I'd don't have any floating buttons but I'd like to pop up whole layout when a snackbar msg is shown. For that I am wrapping my fragment into a CoordinatorLayout like so:

 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/container_for_fragments"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"/>

</android.support.design.widget.CoordinatorLayout>

Where container_for_fragments host my fragments. I declare CoordinatorLayout and then use it like so:

       Snackbar snackbar = Snackbar.make(myCoordinatorLayout, "Hello", Snackbar.LENGTH_LONG);
       snackbar.show();

I am getting: 在此处输入图片说明

Is there a way to pop up whole fragment when snackbar appears?

Write your own behavior, that reacts to showing Snackbar :

public class FrameLayoutBehavior extends CoordinatorLayout.Behavior<FrameLayout> {

    private float mFrTranslationY;

    public FrameLayoutBehavior (Context context, AttributeSet attrs) {
        super();
    }

    @Override
    public boolean layoutDependsOn(final CoordinatorLayout parent, final FrameLayout child, final View dependency) {
        return dependency instanceof Snackbar.SnackbarLayout;
    }

    @Override
    public boolean onDependentViewChanged(final CoordinatorLayout parent, final FrameLayout child, final View dependency) {
        if (dependency instanceof Snackbar.SnackbarLayout) {
            updateFabTranslationForSnackbar(parent, child, dependency);
            ViewCompat.setTranslationY(child, mFrTranslationY);
        }
        return false;
    }

    @Override
    public void onDependentViewRemoved(final CoordinatorLayout parent, final FrameLayout child, final View dependency) {
        super.onDependentViewRemoved(parent, child, dependency);
    }

    private void updateFabTranslationForSnackbar(CoordinatorLayout parent, final FrameLayout fab, View snackbar) {
        mFrTranslationY = getFabTranslationYForSnackbar(parent, fab, snackbar);
    }

    private float getFabTranslationYForSnackbar(CoordinatorLayout parent, FrameLayout fab, View snackbar) {
        float minOffset = 0;
        minOffset = Math.min(minOffset, ViewCompat.getTranslationY(snackbar) - snackbar.getHeight());
        return minOffset;
    }
}

Then add parameter app:layout_behavior to your FrameLayout . It is possible that, when FrameLayout is replaced by Fragment the behavior is lost. Then you will have to add:

@CoordinatorLayout.DefaultBehavior(FrameLayoutBehavior.Behavior.class)

just above class declaration of your fragment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM