From 047d69da9ffcc5fcb7114d172e335209f760b15f Mon Sep 17 00:00:00 2001 From: Jett Hsieh Date: Fri, 22 Mar 2024 07:18:37 -0700 Subject: [PATCH] Fix the sample app about shared elements between fragments for animations Summary: Fix the sample app about shared elements between fragments for animations Differential Revision: D55199237 fbshipit-source-id: df3a44a4463fca589a9e04c54bf5f22bf1741e99 --- .../SharedElementsFragmentActivity.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/sample/src/main/java/com/facebook/samples/litho/java/animations/sharedelements/SharedElementsFragmentActivity.java b/sample/src/main/java/com/facebook/samples/litho/java/animations/sharedelements/SharedElementsFragmentActivity.java index c6198c4be31..7e6fe52ce5f 100644 --- a/sample/src/main/java/com/facebook/samples/litho/java/animations/sharedelements/SharedElementsFragmentActivity.java +++ b/sample/src/main/java/com/facebook/samples/litho/java/animations/sharedelements/SharedElementsFragmentActivity.java @@ -68,6 +68,7 @@ public static class FirstFragment extends Fragment { LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + postponeEnterTransition(); Activity activity = getActivity(); if (activity == null) { @@ -110,6 +111,14 @@ public void onClick(View v) { new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); layout.addView(boxInLithoView); + LithoView.OnDirtyMountListener dirtyMountListener = + new LithoView.OnDirtyMountListener() { + @Override + public void onDirtyMount(LithoView view) { + startPostponedEnterTransition(); + } + }; + boxInLithoView.setOnDirtyMountListener(dirtyMountListener); layout.addView(new View(activity), 200, 200); TextView componentHostTitle = new TextView(activity); @@ -123,8 +132,8 @@ public void onClick(View v) { new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); layout.addView(boxInComponentHost); - layout.setClipChildren(false); + boxInComponentHost.setOnDirtyMountListener(dirtyMountListener); return layout; } @@ -156,6 +165,7 @@ public void nextFragment(View view, int color) { .beginTransaction() .addSharedElement(view, transitionName) .replace(CONTENT_VIEW_ID, fragment) + .setReorderingAllowed(true) .addToBackStack(null) .commit(); } @@ -209,8 +219,13 @@ public void onDirtyMount(LithoView view) { } }; lithoView.setOnDirtyMountListener(dirtyMountListener); - - return lithoView; + // We can not set LithoView as the root view of Fragment because the enforement of alpha + // value here https://fburl.com/code/2ilg549t causes the transition to fail. A workaround is + // to wrap the LithoView in a FrameLayout. + // TODO: T183183765 + final FrameLayout frameLayout = new FrameLayout(activity); + frameLayout.addView(lithoView); + return frameLayout; } } }