我正在尝试在不同活动的片段之间实现共享元素转换,我已经获得了共享元素进入转换,但无法管理反向转换。
片段A托管在活动A中,按钮点击图像作为共享元素添加,活动B在托管片段B的情况下启动,其中包含共享元素的目标视图。
活动A:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
getSupportFragmentManager()
.beginTransaction()
.addToBackStack(null)
.replace(R.id.content, FragmentA.newInstance())
.commit();
}
@Override
public void onBackPressed() {
super.onBackPressed();
finishAfterTransition();
}
片段A:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
postponeEnterTransition();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setSharedElementEnterTransition(TransitionInflater.from(getContext()).inflateTransition(android.R.transition.move));
setSharedElementReturnTransition(TransitionInflater.from(getContext()).inflateTransition(android.R.transition.move));
}
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final ImageView imageView = (ImageView) view.findViewById(R.id.simple_activity_a_imageView);
Button button = (Button) view.findViewById(R.id.simple_activity_a_btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ActivityB.class);
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
getActivity(),
imageView,
ViewCompat.getTransitionName(imageView));
startActivity(intent, options.toBundle());
}
});
}
活动B:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_to_fragment);
getSupportFragmentManager()
.beginTransaction()
.addToBackStack(null)
.replace(R.id.content, FragmentB.newInstance())
.commit();
}
@Override
public void onBackPressed() {
super.onBackPressed();
finishAfterTransition();
}
片段B:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
postponeEnterTransition();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setSharedElementEnterTransition(TransitionInflater.from(getContext()).inflateTransition(android.R.transition.move));
setSharedElementReturnTransition(TransitionInflater.from(getContext()).inflateTransition(android.R.transition.move));
}
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView detailTextView = (TextView) view.findViewById(R.id.simple_activity_b_text);
detailTextView.setText("detail");
ImageView imageView = (ImageView) view.findViewById(R.id.simple_activity_b_image);
imageView.setVisibility(View.VISIBLE);
view.findViewById(R.id.activity_simple_two).setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
imageView.setTransitionName(getString(R.string.simple_activity_transition));
}
Glide.with(this)
.load(GlideFragmentA.ARMADILLO_PIC_URL)
.centerCrop()
.dontAnimate()
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
startPostponedEnterTransition();
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
startPostponedEnterTransition();
return false;
}
})
.into(imageView);
}
答案 0 :(得分:1)
经过严格简化,片段基本上是具有各自生命周期和逻辑的精美ViewGroup,与其他ViewGroup一样,它们也是Activity的视图树的一部分。因此,当查看两个活动之间的转换时,您实际上不是在执行片段级共享元素转换,而是在活动级上执行一个转换。
有必要确保在目标活动中,如果目标视图(共享元素)是添加在顶部的片段的一部分,则当过渡框架开始捕获过渡的最终值时,该目标视图已准备就绪,以便可以在视图层次结构中找到它。
FragmentManager的commit()不会立即执行事务和布局更改,但是会在稍后立即安排它们的时间,在您的情况下,导致上述情况并非如此。将目标活动中的enter转换推迟到片段的视图准备就绪(例如,第一次调用其根布局的onPreDraw())时,应修复该部分。这意味着,您必须在活动参考上在活动B中调用postponeEnterTransition(),而不是片段B,和startPostponedEnterTransition()(Glide加载侦听器的一部分) ,而不是片段B本身。
此外,您需要在活动B上设置共享元素转换(输入和返回),因为实际上是运行它们的组件。设置了这些先决条件后,所需的过渡应该会生效。
答案 1 :(得分:0)
这是因为 imageView 位于片段中...它不是活动B 元素..所以在
活动B你可以做这样的事情并测试它..
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_to_fragment);
FrameLayout frame = (FrameLayout) findViewById(R.id.content);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
frame
.setTransitionName(getString(R.string.simple_activity_transition));
}
getSupportFragmentManager()
.beginTransaction()
.addToBackStack(null)
.replace(R.id.content, FragmentB.newInstance())
.commit();
}
@Override
public void onBackPressed() {
super.onBackPressed();
finishAfterTransition();
}
现在你可以测试一下,即使onBackPressed
会有转换..