您可以考虑使用对而不是这样做。
holder.itemView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(mContext /*your context from main activity*/, Destination.class);
Pair[] pairs = new Pair[3]; /* careful about the number and how many element you want to animate*/
pairs[0]= new Pair<View, String>(holder.X /*item to animate to next activity*/, "transitionX" /* name of transition animation that you have to set on your xml file of the item*/);
pairs[1]= new Pair<View, String>(holder.Y, "transitionY");
pairs[2]= new Pair<View, String>(holder.Z, "transitionZ");
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation((Activity) mContext, pairs);
// start the activity
mContext.startActivity(intent, options.toBundle());
}
});
class CourseViewHolder extends RecyclerView.ViewHolder {
TextView X;
ImageView Y;
LinearLayout Z; /* you can animate layout as well*/
private CourseViewHolder(View itemView) {
super(itemView);
X= itemView.findViewById(R.id.x);
Y=itemView.findViewById(R.id.y);
Z=itemView.findViewById(R.id.z);
}
}
itemView.xml 文件
<TextView
android:id="@+id/x"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transitionName="transitionX"/>
<ImageView
android:id="@+id/x"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transitionName="transitionY"/>
<LinearLayout <!-- you can animate Layout to another object -->
android:id="@+id/Z"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transitionName="transitionZ">
----------------------
--------------------
</LinearLayout>
destinationclass.xml 文件
<TextView
android:id="@+id/x_will_transit_to"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transitionName="transitionX"/> <!-- The name of transition must be the same as java file and both of the element from xml file -->
<!-- You can also animate one type of View to another when transition take place -->
<Button
android:id="@+id/y_will_transit_to"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transitionName="transitionY"/>
<ImageView
android:id="@+id/z__will_transit_to"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transitionName="transitionZ"/>
无论你是从 recyclerview 过渡到 fragment 还是其他什么都没有关系。你只需要在动作中设置动画或 onClick 发生的位置。在将发生转换的两个文件的布局中为转换指定相同的名称。最后,以防万一,检查您正在测试的应用程序的模拟器或移动设备,所有动画比例至少应为 1 倍(在开发人员选项中)。希望它有所帮助,即使它可能不是您正在寻找的答案。