转换动画无法从循环视图工作到细节屏幕

问题描述 投票:1回答:1

我在使用一个简单的过渡动画时遇到了麻烦,我有一个带有多个项目的循环视图,在按下一个项目时,我希望项目上的文本在打开的详细信息屏幕中移动到新位置。我创建了一个github项目,其中包含了我的意思

https://github.com/guylis/android_projects/tree/master/testAnimation/app/src/main/java/guy/testanimation

FragmentA具有带有项目的循环视图片段B具有显示该项目的详细信息屏幕

  • 按下片段中的项目时,适配器会将项目传递给打开详细信息屏幕的片段
  • 我为fragmentA中显示的每个视图定义了一个唯一的转换名称
  • 打开详细信息屏幕时,我首先推迟了动画,创建了相关的移动动画,并且只有在onCreate设置转换名称到相关文本视图后,我继续动画
  • 动画不起作用,文本简单地从一个地方“跳”到另一个地方而不是向它移动

谢谢

android animation android-recyclerview fragment transition
1个回答
0
投票

而不是这样做你可以考虑使用对。

   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的所有动画比例(在开发人员选项中)。希望它有所帮助,即使它可能不是您正在寻找的答案。

© www.soinside.com 2019 - 2024. All rights reserved.