我在使用一个简单的过渡动画时遇到了麻烦,我有一个带有多个项目的循环视图,在按下一个项目时,我希望项目上的文本在打开的详细信息屏幕中移动到新位置。我创建了一个github项目,其中包含了我的意思
FragmentA具有带有项目的循环视图片段B具有显示该项目的详细信息屏幕
谢谢
而不是这样做你可以考虑使用对。
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转换为片段还是其他什么......您只需要在动作中设置动画或在onClick发生的位置。在两个文件的布局中给出转换相同的名称,其中将发生转换。最后,万一,检查您正在测试应用程序的模拟器或移动设备应该具有至少1x的所有动画比例(在开发人员选项中)。希望它有所帮助,即使它可能不是您正在寻找的答案。