嵌套共享元素的转换不会开始从预期的地方 [英] Transition of nested shared elements does not starts from the expected place
问题描述
TL; DR 为什么这种过渡不会启动形式预期的地方吗?
TL;DR Why this transition does not start form expected place?
即,其中是CardView的地方。
That is, the place where is the CardView.
平台/ DEPS:机器人工作室1.0-RC2,TargetSdk 21,MinSdk 21,AppCompat-21.0.2,CardView-21.0.2,RecyclerView-21.0.2
Platform/Deps: Android Studio 1.0-RC2, TargetSdk 21, MinSdk 21, AppCompat-21.0.2, CardView-21.0.2, RecyclerView-21.0.2.
测试的: Genymotion的Nexus 5棒棒堂preVIEW
Tested On: Genymotion Nexus 5 Lollipop Preview
我有三个共同的元素:
来源:
<CardView
android:transitionName="cardView">
<ImageView
android:transitionName="imageView">
<TextView
android:transitionName="textView">
目标:
<RelativeLayout
android:transitionName="cardView">
<ImageView
android:transitionName="imageView">
<TextView
android:transitionName="textView">
请注意下面的转型名编程方式添加。这是因为CardView是holded者在RecyclerView和有它的许多副本。我已经使用相同的名称尝试(如XML实例),但不工作也没有。
Note below that the transition names are added programmatically. This is because CardView are holded on a RecyclerView and there are many copies of it. I've tried using the same name (as in the XML instance) but doesn't work neither.
在ReciclerView.Adapter做到这一点:
The ReciclerView.Adapter does this:
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
//...
ViewCompat.setTransitionName(viewHolder.mCardView, "cardViewTransition" + position);
ViewCompat.setTransitionName(viewHolder.mImageView, "imageTransition" + position);
ViewCompat.setTransitionName(viewHolder.mTextView, "textTransition" + position);
//...
}
在HomeActivity做到这一点:
The HomeActivity does this:
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(android.view.Window.FEATURE_CONTENT_TRANSITIONS);
getWindow().requestFeature(android.view.Window.FEATURE_ACTIVITY_TRANSITIONS);
// ..
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, int position) {
Intent intent = new Intent(selfContext, DetailActivity.class);
View imageView = view.findViewById(R.id.imageView);
TextView textView = (TextView)view.findViewById(R.id.textView);
Bundle extras = new Bundle();
extras.putInt("position", position);
extras.putString("text", textView.getText().toString());
intent.putExtras(extras);
String name = ViewCompat.getTransitionName(view);
ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(selfContext
,Pair.create(view, ViewCompat.getTransitionName(view))
,Pair.create(imageView, ViewCompat.getTransitionName(imageView))
,Pair.create((View)textView, ViewCompat.getTransitionName(textView))
);
ActivityCompat.startActivity(selfContext, intent, options.toBundle());
}
})
);
///...
}
在DetailActivity做到这一点:
The DetailActivity does this:
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(android.view.Window.FEATURE_CONTENT_TRANSITIONS);
getWindow().requestFeature(android.view.Window.FEATURE_ACTIVITY_TRANSITIONS);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
// ...
Bundle extras = getIntent().getExtras();
int mPositionRef = extras.getInt("position");
View base = findViewById(R.id.detail_layout);
View image = findViewById(R.id.imageView);
TextView text = (TextView) findViewById(R.id.txtLabel);
text.setText(extras.getString("text"));
ViewCompat.setTransitionName(base, "cardViewTransition" + mPositionRef);
ViewCompat.setTransitionName(image, "imageTransition" + mPositionRef);
ViewCompat.setTransitionName(text, "textTransition" + mPositionRef);
}
我已经调试和共享的元素具有源和目的地相同的名称。
I've debugged and shared elements have the same names in the source and destination.
为什么这种过渡不会启动形式预期的地方吗?即,其中是CardView的地方。
Why this transition does not start form expected place? That is, the place where is the CardView.
在 GitHub上
推荐答案
有一个在棒棒糖一个已知的错误与嵌套共享的元素(即在同一时间加入这两种视图组和它的孩子中的一个共享的元素),所以它可能涉及到这一点。(来源:?当动画在一个共享的元素活动过渡嵌套的看法毛刺)
There is a known bug in Lollipop related to nested shared elements (i.e. adding both a view group and one of its children as shared elements at the same time), so it might be related to that (source: Glitch when animating nested views in a shared element Activity transition?).
尝试仅仅添加的ImageView和TextView中共享的元素,而不是(不是父相对布局容器)。
Try only adding the ImageView and TextView as shared elements instead (not the parent relative layout container).
这篇关于嵌套共享元素的转换不会开始从预期的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!