您需要为View实现布局行为,并从布局xml文件中引用它.
实现您自己的布局行为很简单.就您而言,您只需要在小吃栏出现时设置视图的平移y即可.
public class YourCustomBehavior extends CoordinatorLayout.Behavior {
public YourCustomBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
child.setTranslationY(translationY);
return true;
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
// we only want to trigger the change
// only when the changes is from a snackbar
return dependency instanceof Snackbar.SnackbarLayout;
}
}
并将其添加到您的布局xml中,如下所示
android:id="@+id/your_sign_up_card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...
app:layout_behavior="com.your.app.YourCustomBehavior"/>
app:layout_behavior属性的值应为行为类的全限定名.
您也可以参考this文章,它很好地解释了所有内容.