-1

I am making a progress bar with icons. I need to make the item shows up in the middle of the screen and then goes to the (horizontal) recyclerveiw. It goes to left to right(The stacked list has no animation but only the new item one).

I've checked animation examples from Google Github and their Document. But I have no idea.

https://developer.android.com/training/animation

https://github.com/android/animation-samples

Is it possible?

c-an
  • 3,543
  • 5
  • 35
  • 82

1 Answers1

0

You can apply an animation when the recyclerview appears.

this is fall down animation example.

  1. make animation

res / anim / layout_animation.xml

<?xml version="1.0" encoding="utf-8"?>
    <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
        android:animation="@anim/item_animation_fall_down"
        android:animationOrder="normal"
        android:delay="15%" />

res / anim / item_animation_fall_down.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500">

    <translate
        android:fromYDelta="-20%"
        android:toYDelta="0"
        android:interpolator="@android:anim/decelerate_interpolator"
        />

    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"
        android:interpolator="@android:anim/decelerate_interpolator"
        />

    <scale
        android:fromXScale="105%"
        android:fromYScale="105%"
        android:toXScale="100%"
        android:toYScale="100%"
        android:pivotX="50%"
        android:pivotY="50%"
        android:interpolator="@android:anim/decelerate_interpolator"
        />

</set>

in your RecyclerView

<android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layoutAnimation="@anim/layout_animation"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />
  1. add animation in your adapter

     public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder>
     {
     private Context context;
    
     // The items to display in your RecyclerView
     private ArrayList<String> items;
     // Allows to remember the last item shown on screen
     private int lastPosition = -1;
    
     public static class ViewHolder extends RecyclerView.ViewHolder
     {
         TextView text;
         // You need to retrieve the container (ie the root ViewGroup from your custom_item_layout)
         // It's the view that will be animated
         FrameLayout container;
    
         public ViewHolder(View itemView)
         {
             super(itemView);
             container = (FrameLayout) itemView.findViewById(R.id.item_layout_container);
             text = (TextView) itemView.findViewById(R.id.item_layout_text);
         }
     }
    
     public CustomAdapter(ArrayList<String> items, Context context)
     {
         this.items = items;
         this.context = context;
     }
    
     @Override
     public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
     {
         View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_item_layout, parent, false);
         return new ViewHolder(v);
     }
    
     @Override
     public void onBindViewHolder(ViewHolder holder, int position)
     {
         holder.text.setText(items.get(position));
    
         // Here you apply the animation when the view is bound
         setAnimation(holder.itemView, position);
     }
    
     /**
      * Here is the key method to apply the animation
      */
     private void setAnimation(View viewToAnimate, int position)
     {
         // If the bound view wasn't previously displayed on screen, it's animated
         if (position > lastPosition)
         {
             Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
             viewToAnimate.startAnimation(animation);
             lastPosition = position;
         }
     }
     }
    

p.s] this is custom_item_layout.xml

<FrameLayout
    android:id="@+id/item_layout_container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/item_layout_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

</FrameLayout>

then you can check animation. But this way make fast scrolling. for fix this, add this.

@Override
public void onViewDetachedFromWindow(final RecyclerView.ViewHolder holder)
{
    ((CustomViewHolder)holder).clearAnimation();
}

and in CustomViewHolder

 public void clearAnimation()
    {
        mRootLayout.clearAnimation();
    }

you can check this site.

c-an
  • 3,543
  • 5
  • 35
  • 82
S T
  • 1,068
  • 2
  • 8
  • 16
  • This isn't what I meant. When I insert an item, it must pop up in the middle of the display(larger size) and it moves to the last position of the horizontal recyclerview(progress bar). there must be an linear or curve animation when the new item goes to the last position of the recyclerview. And also the size also should be manageable (large when it pops up but becoming smaller and smaller and then fit the progress bar.) – c-an Nov 18 '20 at 05:39
  • I think you might think about **Shared Transition Animation**. then check this. https://medium.com/@j.c.moreirapinto/recyclerview-shared-transitions-in-android-navigation-architecture-component-16eb902b39ba – S T Nov 18 '20 at 05:44
  • @S T I think It's different because I want it happens in an activity. The progress bar view (recyclerview) will stay and I continue answering the questions in the main view(below the recyclerview). – c-an Nov 18 '20 at 05:47