I have a recycled view which is populated with CardView layouts. I've noticed that in many apps, these items have a nice animated entrance rather than abruptly appearing as they currently do in my application. An example of what I'm trying to achieve is shown below:
How would I use my swipeRefreshListener/adapter to trigger this animation? How would I implement this through my adapter?
Note: I need this animation to also be triggered when the RecyclerView is first populated with the CardView items, and the data they contain.
My RecyclerView's adapter is shown below:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private String[] mDataset;
private String[] mClassDataset;
private int lastPosition = -1;
private Context context;
View view;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public TextView mClassDataView;
CardView container;
public ViewHolder(View itemView) {
super(itemView);
mTextView = (TextView)itemView.findViewById(R.id.grade);
mClassDataView = (TextView)itemView.findViewById(R.id.class_name);
container = (CardView)itemView.findViewById(R.id.card_view);
}
}
public RecyclerAdapter(String[] myDataset, String[] classData, Context context) {
mDataset = myDataset;
mClassDataset = classData;
this.context = context;
}
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_item, parent, false);
ViewHolder vh = new ViewHolder(v);
view = v;
return vh;
}
@Override
public int getItemCount() {
return mClassDataset.length;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
if (mDataset.length < mClassDataset.length) {
holder.mTextView.setText("N/A");
holder.mClassDataView.setText(mClassDataset[position]);
if (mClassDataset[position].equals("N/A")) {
}
} else {
holder.mTextView.setText(mDataset[position]);
holder.mClassDataView.setText(mClassDataset[position]);
}
for (int i = 0; i < getItemCount(); i++) {
animate(view, i);
}
}
private void animate(final View view, final int position){
Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
view.startAnimation(animation);
lastPosition = position;
}
}
The animation is currently working, however the thing is the items are being animated all at once. I tried iterating through the items by creating a custom getChild()
method, but that didn't work. Somehow I need to access/animate each child individually and possibly set a delay between each animation. However, I'm not sure how exactly I can accomplish this. I know I can use a Handler to set delay, but accessing each child is a problem.