是否可以将 CoordinatorLayout 与 ConstraintLayout 结合使用?

Is it possible to combine CoordinatorLayout with ConstraintLayout?

在我的一个视图中,我的主视图底部有一个 ConstraintLayout,其中包含三个 ImageButton。我还使用 Snackbar 在底部显示一些信息。如果 Snackbar 可见,我想向上移动完整的 ConstraintLayout。我现在尝试了不同的东西但没有任何结果。在一个单独的 Android-Project 中,我对 CoordinatorLayouts 进行了一些尝试,到目前为止我的结果是,ConstraintLayouts 是不可能的。我的假设对吗?

这是有效的

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);
    }
}

这是行不通的

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);
    }
}

ConstraintLayout 是否有任何机会在显示 Snackbar 时做出反应?

编辑:

这是我当前的布局

<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>

我希望只有第二个 ConstraintLayout 受行为影响。我只为第一个ConstraintLayout得到它,但结果不是我想要的。

花了一些时间,但我找到了解决问题的方法,我想这个解决方法对其他人也有帮助。

诀窍是深入挖掘自定义行为-Class。看来最外层的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;
    }