我正在尝试创建从单个RecyclerView项目到新DetailsFragment的转换。 这是我的情况:
布局层次结构:
(Lvl1) MainActivity (fragment_container)
(Lvl2) HomeFragment
(Lvl3) ViewPager
(Lvl4) AnotherFragment
(Lvl5) RecyclerView
(Lvl6) CardView (transitionName="item")
(Lvl2) DetailsFragment
(Lvl3) RelativeLayout (transitionName="item")
因此,当单击RecyclerView中的项目时,fragmentTransaction将使用DetailsFragment替换HomeFragment,我想要实现的是" changeBounds"从CardView到RelativeLayout,而HomeFragment在fadingIn中是fadingOut和DetailsFragment。
我的解决方案可以在下面看到,我遇到的问题是DetailsFragment会立即显示在事务提交的后台。单击的CardView仍然可见(在DetailsFragment上方),它正在消失,但没有任何尺寸或位置的变化。在此操作过程中,所有其他CardView都是不可见的。
很明显,我不会在这里发现任何事情并且它不能正常工作......
activity_main.xml中
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
MainActivity.java - &gt; OnRecyclerViewItemClick
@Override
public void onRecyclerViewClick(int position, int key) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Fade fadeOutTransition = new Fade(Fade.OUT);
fadeOutTransition.setDuration(2000);
Fade fadeInTransition = new Fade(Fade.IN);
fadeInTransition.setStartDelay(2000);
fadeInTransition.setDuration(10000);
ChangeBounds changeBoundsTransition = new ChangeBounds();
changeBoundsTransition.setDuration(10000);
homeFragment.setSharedElementEnterTransition(changeBoundsTransition);
homeFragment.setReenterTransition(fadeInTransition);
homeFragment.setExitTransition(fadeOutTransition);
frag.setEnterTransition(fadeInTransition);
frag.setAllowEnterTransitionOverlap(false);
frag.setAllowReturnTransitionOverlap(false);
frag.setSharedElementEnterTransition(changeBoundsTransition);
ViewPager pager = (ViewPager)((FrameLayout)homeFragment.getView()).getChildAt(0);
RelativeLayout pagerLayout = (RelativeLayout) pager.getChildAt(0);
RecyclerView recycler = (RecyclerView)pagerLayout.getChildAt(0);
View view = recycler.getChildAt(position);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, frag)
.addToBackStack(null)
.addSharedElement(view , "item")
.commit();