简体   繁体   中英

Android Fragment Share Element Transition with Hide and Show

I am trying to make Share Element Transition for RecyclerView element , using the image as the share element.

I am able to make other transition work except the Share Element Transition.

my guess is that the problem is on in Transition.hide and transition.Show.

other that that i am stuck. please help :)

Here is the method that handles the transition

 public void goToProduct(ProductItem current) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Transition changeTransform = TransitionInflater.from(this). inflateTransition(R.transition.change_image_transform); Transition explodeTransform = TransitionInflater.from(this). inflateTransition(android.R.transition.explode); productListFragment.setSharedElementReturnTransition(changeTransform); productListFragment.setExitTransition(explodeTransform); productPageFragment.setSharedElementEnterTransition(changeTransform); productPageFragment.setEnterTransition(explodeTransform); isProductPageOpenedFromCart = false; isProductPageOpenedFromList = false; ImageView listImage = (ImageView) findViewById(R.id.listImg1); ImageView pageImage = (ImageView) findViewById(R.id.pageImg); FragmentTransaction transaction = manager.beginTransaction(); transaction.hide(productListFragment) .addToBackStack("transaction") .addSharedElement(listImage, "MyTransition"); if (isCartOpen) { CartFragment cartFragment = (CartFragment) manager.findFragmentByTag(CART_FRAGMENT_TAG); transaction.remove(cartFragment); isCartOpen = false; isProductPageOpenedFromCart = true; } else { isProductPageOpenedFromList = true; } productPageFragment.setProduct(current); transaction.show(productPageFragment).addToBackStack("transaction") .addSharedElement(pageImage, "MyTransition");; transaction.commit(); } else { 

the transition folder file is named change_image_transform and contains:

 <?xml version="1.0" encoding="utf-8"?> <transitionSet xmlns:android="http://schemas.android.com/apk/res/android"> <changeTransform/> <changeImageTransform/> </transitionSet> 

I also did use android:transitionName="MyTransition" on boath images.

And here are lines of code added to style.xml

 <item name="android:windowContentTransitions">true</item> <item name="android:windowEnterTransition">@transition/change_image_transform</item> <item name="android:windowExitTransition">@transition/change_image_transform</item> <item name="android:windowSharedElementEnterTransition">@transition/change_image_transform</item> <item name="android:windowSharedElementExitTransition">@transition/change_image_transform</item> 

In RecyclerView you propably have many views with id R.id.listImg1 You have to in adapter set sharedView setTransitionName programatycally for example you can use your product id for creating transition name. And when you creating second details fragment you can pass by bundle transition name to that gragment. In onCrete you can set image transitionname to the same like before.

Or you can create:

 public interface IRecyclerClikListener {

   public void onItemClik(int pos,Object obj,View view);
 }

Add this listener to your recycler. To the view you pass a refrence to clikced row. If you have reference to cliked view you can set transition name to image belongs to this view. Dont forget to set the same transition name in second fragment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM