问题描述
我试图动画2从选定的项目在RecyclerView简单的浏览到一个新的片段。我已经看了很多动画共享元素从一个活动到另一个活动的例子,但在同一活动中从一个片段一个共享的元素动画到另一个片段的例子非常少。它几乎工作。
I'm trying to animate 2 simple Views from a selected item in a RecyclerView to a new fragment. I've looked at a lot of examples of animating shared elements from one Activity to another Activity, but very few examples of animating a shared element from one Fragment to another Fragment within the same Activity. It almost works.
下面是我的结构。
活动
- 全屏片段1与RecyclerView
-- Full screen Fragment1 with RecyclerView
- 全屏Fragment2细节
-- Full screen Fragment2 with details
当用户选择的片段1的RecyclerView一个项目,我替换片段1与Fragment2具有在不同的位置和大小在它的共享元素的视图。
When the user selects an item in the RecyclerView in Fragment1, I replace Fragment1 with Fragment2 that has a View with the shared elements in it in different positions and sizes.
有一个有点一招,以得到它的工作,你必须确保你的transitionName是唯一的在列表中的每个项目,当然这transitionName必须Fragment2元素的transitionName匹配动画播放。我有这部分的工作,当我选择一个项目,2个共享意见做动画,只是不完全了解你所期望的2做活动的时候了。
There's a bit of a trick to get it to work, you have to make sure your transitionName is unique for each item in your list, and of course that transitionName must match the transitionName of the element in Fragment2 for the animation to play. I have this part working, when I select an item, the 2 shared Views do animate, just not exactly how you would expect when doing it between 2 Activities.
如果我选择在屏幕的底部附近的一个项目,它吸引了观为Fragment2和动画的共享2次,如果他们在该项目在屏幕的顶部。很难解释。这里有一些图片
If I select an item near the bottom of the screen, it draws the View for Fragment2 and animates the 2 shared Views as if they were in the item at the top of the screen. Hard to explain. Here are some pictures
片段1
Fragment1
Fragment2
Fragment2
在两个片段,我设置以下
In both fragments I'm setting the following
setSharedElementEnterTransition(new ChangeBounds());
setSharedElementReturnTransition(new ChangeBounds());
setAllowEnterTransitionOverlap(true);
setAllowReturnTransitionOverlap(true);
在的onCreate(),我给自己定其父活动也
Also in their parent Activity in onCreate() I've set
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
为什么我的共享元素的动画也开始在我的屏幕顶部甚至当他们在我的屏幕的底部开始在选定的项目你知道吗?
Any idea why my shared element animations are starting at the top of my screen even when the they were starting in the selected item at the bottom of my screen?
推荐答案
终于解决了这个问题!事实证明,因为认为我2片段之间共享是在第二个片段,另一种观点认为(RelativeLayout的)的孩子,你需要将ChangeTransform过渡添加到您的TransitionSet。显然ChangeTransform告诉系统在第二个片段中的新位置,动画之前要记得原来的位置在第1段的意见。这是我的更新transitionSet。我也将收拾我的测试项目codeA位,并作出最后冲刺的到位桶情况下,它会帮助别人之后我。感谢所有帮助这一个亚历克斯,感谢您@乔治贴装回答某人类似的问题,即一指点给我这个解决方案。
Finally solved this problem! As it turns out because the view I'm sharing between 2 fragments is a child of another view (RelativeLayout) in the 2nd fragment, you need to add the ChangeTransform transition to your TransitionSet. Apparently ChangeTransform tells the system to remember the views original position in the 1st fragment before animating to the new position in the 2nd fragment. Here is my updated transitionSet. I'll also clean up my test project code a bit and make a final push to bitbucket in case it will help others after me. Thanks for all the help with this one Alex and thank you to @George-mount for answering someones similar question that dropped the hint to me for this solution.
<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<changeTransform/>
<changeBounds/>
</transitionSet>
https://bitbucket.org/brockoli/fragmentsharedelements
这篇关于共享的元素片段之间动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!