android - Is it possible to combine CoordinatorLayout with ConstraintLayout?
问题描述
In one of my views I have a ConstraintLayout on the bottom of my mainview with three ImageButtons inside of it. I also use a Snackbar to show some informations on the bottom. I want to move the complete ConstraintLayout up if the Snackbar is visible. I tried now different things without any result. In a separate Android-Project i played a little bit around with CoordinatorLayouts and my result so far is, that it is not possible for ConstraintLayouts. Is my assumption right?
This is working
public class CustomBehaviourButton extends CoordinatorLayout.Behavior<Button>{
public CustomBehaviourButton() {
}
public CustomBehaviourButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull Button child, @NonNull View dependency) {
return dependency instanceof Snackbar.SnackbarLayout;
}
@Override
public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull Button child, @NonNull View dependency) {
float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
child.setTranslationY(translationY);
return true;
}
@Override
public void onDependentViewRemoved(@NonNull CoordinatorLayout parent, @NonNull Button child, @NonNull View dependency) {
float translationY = Math.min(0, dependency.getTranslationY() + dependency.getHeight());
child.setTranslationY(translationY);
}
}
And this is not working
public class CustomBehaviourConstraintLayout extends CoordinatorLayout.Behavior<ConstraintLayout> {
public CustomBehaviourConstraintLayout() {
}
public CustomBehaviourConstraintLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull ConstraintLayout child, @NonNull View dependency) {
return dependency instanceof Snackbar.SnackbarLayout;
}
@Override
public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull ConstraintLayout child, @NonNull View dependency) {
float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
child.setTranslationY(translationY);
return true;
}
@Override
public void onDependentViewRemoved(@NonNull CoordinatorLayout parent, @NonNull ConstraintLayout child, @NonNull View dependency) {
float translationY = Math.min(0, dependency.getTranslationY() + dependency.getHeight());
child.setTranslationY(translationY);
}
}
Is there by any chance a way for a ConstraintLayout to react if a Snackbar is shown??
Edit:
This is my current Layout
<androidx.coordinatorlayout.widget.CoordinatorLayout>
<androidx.constraintlayout.widget.ConstraintLayout>
<!-- different widgets -->
<androidx.constraintlayout.widget.ConstraintLayout>
<!-- Some Buttons -->
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I want that only the second ConstraintLayout is effected by the behaviour. I get it only for the first ConstraintLayout, but the result is not what I want.
解决方案
花了一段时间,但我找到了解决我的问题的方法,我想这个解决方案对其他人有帮助。
诀窍是深入挖掘自定义行为类。看来最外面的ConstraintLayout需要得到Bahviour。在那种情况下,我只是在onDependendViewChanged中引用了我最里面的 ConstraintLayout来只修改我的布局的这一部分。
@Override
public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull ConstraintLayout child, @NonNull View dependency) {
ConstraintLayout mainControlsPanel = child.findViewById(R.id.mainControlsPanel);
float heightDiff = dependency.getY() - mainControlsPanel.getY();
float translationY = heightDiff - mainControlsPanel.getHeight() - MARGIN;
mainControlsPanel.setTranslationY(translationY);
return true;
}
推荐阅读
- python - 在具有 <= 1 MB RAM 和 <= 10 MB ROM 的设备上运行支持向量机内核是否可行?
- angular - Angular 6 ng2 文件上传无法将队列的属性读取为未定义
- powershell - win_shell 完成命令但未结束进程
- mysql - MySQL:一个简单的正则表达式没有按预期工作
- ios - 使用缓存的 UICollectionViewLayoutAttributes 子类化 UICollectionViewLayout
- c# - 使用指向 Func<> 的指针的分配方法
- java - 在运行时或编译时评估/解析左右对象?
- oracle - Powershell - 如何使用 Oracle Data Provider for .NET (ODP.NET) 将查询导出到 CSV 文件
- internet-explorer - 链接中的 SVG 跨度在 IE 中不起作用
- macos - AppleScript 遍历 Outlook 消息 - 收到的时间缺失值