首页 > 解决方案 > 删除小吃店时向下移动视图/按钮

问题描述

所以我有以下基本代码,以确保在出现小吃店时我的按钮会上升:

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>

结果如下所示:

正确的结果

效果很好,但是问题是,如果我通过向左/向右滑动来移除小吃栏,按钮将保持在“上部”位置:

按下按钮

我怎样才能检测到我的小吃店的 MovingDown/Sliding 到正确的行为。我错过了什么重要的事情吗?

标签: androidmaterial-designandroid-snackbarsnackbarattachedbehaviors

解决方案


推荐阅读