我可以为constraintLayout动画完成时设置回调吗

时间:2019-03-06 09:47:38

标签: android

我刚刚开始学习动画,并且我试图在约束布局更改动画完成后添加另一个视图。 我添加了一个侦听器,用于查看约束布局何时发生更改,但在动画开始时而不是在动画完成后触发。

        rootConstraintLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                Log.d(TAG, "onLayoutChange: testing ");
            }
        });

这是我用来更改布局的方法

    private void changeExamplePageLayout() {

        ConstraintSet constraintSetBeforeExample = new ConstraintSet();
        ConstraintSet constraintSetAfterExample = new ConstraintSet();

        constraintSetBeforeExample.clone(rootConstraintLayout);

        constraintSetAfterExample.clone(getContext(), R.layout.example_fragment_sub_set);

        TransitionManager.beginDelayedTransition(rootConstraintLayout);

        constraintSetAfterExample.applyTo(rootConstraintLayout);

    }

我应该看的任何方向都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

所以我解决了这个问题,

代替这里的代码=

rootConstraintLayout = view.findViewById(R.id.rootConstraintLayout);

constraintSetBeforeExample = new ConstraintSet();
constraintSetAfterExample = new ConstraintSet();


constraintSetBeforeExample.clone(rootConstraintLayout);
constraintSetAfterExample.clone(getContext(),R.layout.example_fragment_sub_set);

TransitionManager.beginDelayedTransition(rootConstraintLayout);

如果您未在delayedTransition方法中提供过渡,则过渡管理器将使用默认过渡,我创建了自己的过渡,可以向其添加侦听器。

rootConstraintLayout = view.findViewById(R.id.rootConstraintLayout);

constraintSetBeforeExample = new ConstraintSet();
constraintSetAfterExample = new ConstraintSet();


constraintSetBeforeExample.clone(rootConstraintLayout);
constraintSetAfterExample.clone(getContext(),R.layout.example_fragment_sub_set);


Transition transition = new ChangeBounds();

transition.addListener(new Transition.TransitionListener() {
        @Override
        public void onTransitionStart(@NonNull Transition transition) {

        }

        @Override
        public void onTransitionEnd(@NonNull Transition transition) {

            if (layoutChanged) {

                addExampleQuestionFragment();

            }


        }

        @Override
        public void onTransitionCancel(@NonNull Transition transition) {

        }

        @Override
        public void onTransitionPause(@NonNull Transition transition) {

        }

        @Override
        public void onTransitionResume(@NonNull Transition transition) {

        }
    });

然后将您刚刚创建的转换设置为方法中的参数

TransitionManager.beginDelayedTransition(rootConstraintLayout, transition);

如果其他人遇到此问题,请帮助他