I have a TextView inside a Constraint Layout. I am trying to animate in a way that the view goes off the screen from top. This is what I did so far,
ConstraintSet constraintSet = new ConstraintSet(); ConstraintLayout layout = (ConstraintLayout)holder.mView; constraintSet.clone(layout); constraintSet.clear(R.id.txt_PackageTitle,ConstraintSet.TOP); constraintSet.clear(R.id.txt_PackageDescription,ConstraintSet.TOP); constraintSet.clear(R.id.txt_PackageTitle,ConstraintSet.BOTTOM); constraintSet.clear(R.id.txt_PackageDescription,ConstraintSet.BOTTOM); constraintSet.setMargin(R.id.txt_PackageTitle,ConstraintSet.TOP,-600); constraintSet.setMargin(R.id.txt_PackageDescription,ConstraintSet.TOP,-1200); ChangeBounds transition = new ChangeBounds(); transition.setInterpolator(new BounceInterpolator()); transition.setDuration(600); TransitionManager.beginDelayedTransition(layout,transition); constraintSet.applyTo(layout);
Now this code only moves the contents to the very top of the view, its not going out of the view and getting disappeared.
How can I do this with constraint layout?
Answer
Instead of clearing the bottom constraint of the TextView
, try constraining its bottom to the top of the ConstraintLayout
like this:
constraintSet.connect (R.id.txt_PackageTitle, ConstraintSet.BOTTOM, PARENT_ID, ConstraintSet.TOP);
Now when the view is animated, it should slide off the top edge.
Negative margins are not supported with ConstraintLayout
as is noted here.