ConstraintSet 获取现有连接

ConstraintSet get existing connection

如何从约束集中获取给定方向的当前连接视图?

我正在尝试在另一个视图之前插入一个视图

private void insertBefore(int toInsert,int insertBefore)
{
            ConstraintSet cs = new ConstraintSet();

            ConstraintLayout root = (ConstraintLayout)findViewById(R.id.constraintRoot);
            cs.clone(root);

            int currentAbove = cs.getConnectedView(ConstraintSet.TOP);//this is the part I can figure out

            cs.connect(insertBefore,ConstraintSet.TOP,toInsert,ConstraintSet.BOTTOM);
            cs.connect(toInsert,ConstraintSet.TOP,currentAbove,ConstraintSet.BOTTOM);

            root.setConstraintSet(cs);
}

以上或多或少是我正在尝试做的事情。我在 ConstraintSet Documentation 中找不到任何提及与上述函数类似的函数 getConnectedView()

我可以向函数添加一个 insertAfter 参数,但我宁愿避免这样做。

这是我想要完成的图表

看看这是否适合你

private void insertBefore(@IdRes int toInsert, @IdRes int insertBefore) {
    ConstraintSet cs = new ConstraintSet();
    ConstraintLayout root = (ConstraintLayout) findViewById(R.id.constraintRoot);

    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) findViewById(insertBefore).getLayoutParams();

    cs.clone(root);

    cs.clear(toInsert, ConstraintSet.END);
    cs.clear(toInsert, ConstraintSet.START);

    cs.connect(toInsert, ConstraintSet.TOP, params.topToBottom != -1 ? params.topToBottom : params.topToTop, ConstraintSet.BOTTOM, 0);
    cs.connect(toInsert, ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
    cs.connect(toInsert, ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0);

    cs.connect(insertBefore, ConstraintSet.TOP, toInsert, ConstraintSet.BOTTOM, 0);
    cs.connect(insertBefore, ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
    cs.connect(insertBefore, ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0);

    cs.applyTo(root);
}