【问题标题】:transition animation not working from recycled view to detail screen过渡动画无法从回收视图到详细信息屏幕
【发布时间】:2019-02-11 16:39:26
【问题描述】:

我在使用简单的过渡动画时遇到问题,其中我有一个包含多个项目的回收视图,并且在按下一个项目时,我希望项目上的文本在打开的详细信息屏幕中向上移动到其新位置。 我创建了一个 github 项目,其中包含我的意思的示例

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

FragmentA 具有项目的回收视图 片段 B 有一个显示该项目的详细信息屏幕

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

谢谢

【问题讨论】:

    标签: android animation android-recyclerview fragment transition


    【解决方案1】:

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

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

    【讨论】:

    • 嗨,我相信这不是问题,我将带有代码的示例项目上传到原始答案以使其更清晰。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2021-11-21
    • 2021-09-06
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多