手动关闭 snackbar 时向下移动按钮

Move Button down when dissmising snackbar manually

所以我有以下基本代码,可确保在出现小吃栏时我的按钮会弹起:

public class MoveUpwardBehavior extends CoordinatorLayout.Behavior<View> {
    private static final boolean SNACKBAR_BEHAVIOR_ENABLED;

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

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
        child.setTranslationY(translationY);
        return true;
    }

    static {
        SNACKBAR_BEHAVIOR_ENABLED = Build.VERSION.SDK_INT >= 11;
    }
}

我的自定义线性布局:

public class CustomLinearLayout extends LinearLayout implements CoordinatorLayout.AttachedBehavior {

    MoveUpwardBehavior mb = new MoveUpwardBehavior();
    public CustomLinearLayout(Context context) {
        super(context);
    }

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

    public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @NonNull
    @Override
    public CoordinatorLayout.Behavior getBehavior() {
        return mb;
    }
}

最后但同样重要的是我的布局:

<?xml version="1.0" encoding="utf-8"?>
 <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 app:layout_behavior=".pkgActivity.MoveUpwardBehaviour"
 tools:context=".pkgTestforend.DriverListFragment">


    <ListView
        android:id="@+id/listAllDrivers"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>


    <com.example.dochjavatestimplementation.pkgTestforend.CustomLinearLayout
        android:layout_width="match_parent"
        android:id="@+id/cusLL"
        android:layout_height="match_parent"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/btnOpenDriverAddFragment2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/baseline_person_add_24"
                />

        </RelativeLayout>

    </com.example.dochjavatestimplementation.pkgTestforend.CustomLinearLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

这工作得很好,但问题是当手动关闭快餐栏时,按钮仍然存在:

为了解决这个问题,我尝试了以下方法:

Snackbar kd = Snackbar.make(customLinearLayout, "Text to display", Snackbar.LENGTH_LONG)
    .addCallback(new Snackbar.Callback() {

        @Override
        public void onDismissed(Snackbar snackbar, int event) {
            if (event == Snackbar.Callback.DISMISS_EVENT_SWIPE) {
                btnOpenDriverAddFragment2.setTranslationY(90); //lower button down manually!
                }                                        
            }

            @Override
            public void onShown(Snackbar snackbar) {
            }
        });

kd.show();

然而,这不是很好,因为小吃店似乎仍然 visible/button 被关闭的小吃店覆盖?

为什么会这样,小吃店基本上仍然可见但被关闭了?

btnOpenDriverAddFragment2.setTranslationY(90); 更改为 customLinearLayout.setTranslationY(0); 将解决您的问题。

因为不仅按钮被向上移动而且自定义线性布局也是按钮的父级,所以您只需要重置父级的 y 位置。 你只是改变按钮的y值,但你忘记了线性布局。