使用ViewPager从活动到片段的共享元素过渡 [英] Shared element transition from Activity to Fragment using ViewPager

查看:127
本文介绍了使用ViewPager从活动到片段的共享元素过渡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动中使用此代码

am using this code in my Activity

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        view.setTransitionName("pic");

    ActivityOptionsCompat a=ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) context,view,view.getTransitionName());

    Intent i3=new Intent(context,MovieInfo.class);
    i3.putExtra("id",view.getId());
    startActivity(i3,a.toBundle());
    }

并且在我的Fragment Class中正在使用

and in my Fragment Class am using this

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        getActivity().getWindow().setSharedElementExitTransition(TransitionInflater.from(getActivity()).inflateTransition(R.transition.shared_element_transition));
    }
} 

我也尝试在主要活动中使用它,但无法实现共享元素转换,但是当我返回活动转换时,运行良好意味着其余代码很好!
任何帮助将非常感谢!

I also tried Using it in the main activity but not able achieve shared element transition but when i go back to my activity transition is working well implies rest of the code is fine!
Any help would be great thanks!

推荐答案

首先,您需要在style.xml中的应用程序主题"中启用windowContentTrasition

First you need to enable windowContentTrasition in your App Theme in styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  <!-- Customize your theme here. -->
  <item name="android:windowContentTransitions">true</item>
  .
  .
</style>

接下来将相同的transitionName属性添加到两个布局中的两个共享元素

Next add same transitionName property to both shared elements in both the layouts

android:transitionName="transition_name"

在您的活动中创建这样的意图

In your activity create an intent like this

ActivityOptionsCompat options = ActivityOptionsCompat.
    makeSceneTransitionAnimation(this, view, "transition_name");

Intent intent = new Intent(this, MovieInfo.class); 
startActivity(intent, options.toBundle());

这篇关于使用ViewPager从活动到片段的共享元素过渡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆