When switching to a fragment, the animation does not work

Here is the code:

@Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View contentView = inflater.inflate(R.layout.activity_main2, container, false); slideUp = contentView.findViewById(R.id.slideup); slideDown = contentView.findViewById(R.id.slideDown); line_item = contentView.findViewById(R.id.scan_item); slideDown.animate().setDuration(DURATION).translationYBy(slideDown.getHeight()); return contentView; } 

    1 answer 1

    Your animation works out before you see anything on the screen, transfer the animation to the onResume() method.

    UPDATE

    slideDown.getHeight() in onCreate() will return 0 to you, just as if you try to do it right away in onResume()

    Hang the kelbeck on the Обсервер in onCreate() (or onResume() - depending on how you please) on your view.

     slideDown.getViewTreeObserver().addOnGlobalLayoutListener(this); 

    and in Kelbeck already directly work out the animation

     @Override public void onGlobalLayout() { slideDown.getViewTreeObserver().removeGlobalOnLayoutListener(this); slideDown.animate().setDuration(DURATION).translationYBy(slideDown.getHeight()); } 
    • I tried it. Did not help. - Rasul A-
    • @ RasulA-ev updated the answer - Chaynik