回收器视图和查看页之间的共享元素转换故障



嗨,我制作了一个应用程序,在其中使用共享元素过渡在片段中的回收器视图和处于活动中的滑块视图页面之间的一些动画。 现在的问题是,当我应用共享元素过渡并单击 recyclerview 时,它会闪烁片刻,细节活动开始但没有动画,当我按下后退按钮时,共享元素动画发生,但这也非常小故障。 故障我的意思是当我按下卡片视图的白色背景弹出然后图像出现时。 为此,我在 youtube 上看到了一些教程,并在谷歌上搜索了一些相关的 So 问题,但仍然无法弄清楚我在哪里出了问题,因为我为两个图像视图提供了相同的过渡名称。 所以如果有人可以在这里帮助我,请。 谢谢

这是我的回收器视图和样式的代码.xml

样式.xml

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowContentTransitions" tools:targetApi="lollipop">true</item>
<item name="android:windowAllowEnterTransitionOverlap" tools:targetApi="lollipop">true</item>
<item name="android:windowAllowReturnTransitionOverlap" tools:targetApi="lollipop">true</item>
<item name="android:windowSharedElementEnterTransition" tools:targetApi="lollipop">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition" tools:targetApi="lollipop">@android:transition/move</item>
</style>
</resources>

视图持有人

public class VH extends RecyclerView.ViewHolder implements View.OnClickListener{
ImageView iv;
TextView tv;
HWRatioContainer ivc;
Context context;
int[] images;
private AtomicBoolean enterTransitionStarted;
public static final String EXTRA_TRANSITION_IMAGE = "image";
CustomButton top_recycler_button;
public VH(View itemView,Context context,int[] images) {
super(itemView);
iv = (ImageView) itemView.findViewById(R.id.iv);
this.context = context;
this.images = images;
this.enterTransitionStarted = new AtomicBoolean();
top_recycler_button = (CustomButton)itemView.findViewById(R.id.top_recycler_button);
itemView.setOnClickListener(this);
//tv = (TextView) itemView.findViewById(R.id.tv);
///ivc = (HWRatioContainer) itemView.findViewById(R.id.ivc);
/*ivc.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
ivc.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
ivc.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
ivc.setTranslationX(ivc.getWidth() >> 4);
}
});*/
}
@SuppressLint("RestrictedApi")
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View view) {
Intent intent = new Intent(context,DetailsActivity.class);
intent.putExtra("slider_image",images[getAdapterPosition()]);
iv.setTransitionName(context.getString(R.string.first_page_transition));
ActivityOptionsCompat compat = ActivityOptionsCompat.makeSceneTransitionAnimation((MainActivity)context,iv,ViewCompat.getTransitionName(iv));
context.startActivity(intent,compat.toBundle());
}
}

以及正在活动的视图页机适配器的代码

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.custom_dots_layout,null);
imageView = (ImageView)view.findViewById(R.id.detail_viewpager_image);
//Intent intent  = new Intent();
imageView.setImageResource(getImage());
if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP){
imageView.setTransitionName(context.getString(R.string.first_page_transition));
}
ViewPager viewPager1 = (ViewPager)container;
viewPager1.addView(view,0);
return view;
}

我把它作为一个答案发布,因为它有点长。你是对的,例如,如果您想在片段活动之间共享一个图像,您需要在两者中提供相同的名称。但在您的情况下,您有一个可以包含许多元素的RecyclerView,因此您需要为每个元素提供唯一的过渡名称。一个好的地方是onBindViewHolder您可以通过附加位置来设置过渡名称,例如:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
imageView.setTransitionName("YourTransitionName" + position);
// other code
}

这样,您就可以确保每个转换名称都是唯一的,因此没有冲突。如您所知,您需要在两个地方设置转换名称,因此您需要将位置传递给您的ViewPager。另外,请查看这篇文章,它可能会有所帮助。

最新更新