How to Stop Motion Layout animation when data is fit to screen

Viewed 140

I used Motion Layout for Collapse Layout. Motion layout working perfect as I need but I want to set motion layout animation based on data fit too the screen.

Like if data fit in screen then no need to animation. If data out of the screen then show animation.

2 Answers

Finally I got solution:

motionLogin?.enableTransition(R.id.transitionLogin, false)
        constraintLayout.viewTreeObserver.addOnGlobalLayoutListener(object :
            ViewTreeObserver.OnGlobalLayoutListener {

            override fun onGlobalLayout() {
                // If the scrollView can scroll, disable the accept menu item button
                if (constraintLayout.canScrollVertically(1) || constraintLayout.canScrollVertically(
                        -1
                    )
                ) {
                    motionLogin?.enableTransition(R.id.transitionLogin, true)
                }

                // Remove itself after onGlobalLayout is first called or else it would be called about a million times per second
                constraintLayout.viewTreeObserver.removeOnGlobalLayoutListener(this)
            }
        })

For Java version:

 public static void enableDisableTransition(MotionLayout motionLayout, RecyclerView recyclerView){
    motionLayout.enableTransition(R.id.transition, false);
    recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (recyclerView.canScrollVertically(1) || recyclerView.canScrollVertically(-1 )
            ) {
                motionLayout.enableTransition(R.id.transition, true);
            }
            recyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });
}
Related