Skip to content

Instantly share code, notes, and snippets.

@Mustufa786
Created August 5, 2019 06:59
Show Gist options
  • Save Mustufa786/a08da305529c1fd33a7bf8772bcfad26 to your computer and use it in GitHub Desktop.
Save Mustufa786/a08da305529c1fd33a7bf8772bcfad26 to your computer and use it in GitHub Desktop.
public class RecyclerAnimationAdapter extends RecyclerView.Adapter<RecyclerAnimationAdapter.ViewHolder> {
Context context;
List<Person> list;
long DURATION = 500;
private boolean on_attach = true;
public RecyclerAnimationAdapter(Context context, List<Person> personList) {
this.context = context;
this.list = personList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item_list, null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int i) {
holder.name.setText(list.get(i).name);
Picasso.get().load(list.get(i).image).into(holder.imageView);
setAnimation(holder.itemView, i);
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
Log.d(TAG, "onScrollStateChanged: Called " + newState);
on_attach = false;
super.onScrollStateChanged(recyclerView, newState);
}
});
super.onAttachedToRecyclerView(recyclerView);
}
private void setAnimation(View itemView, int i) {
if(!on_attach){
i = -1;
}
boolean isNotFirstItem = i == -1;
i++;
itemView.setAlpha(0.f);
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator animator = ObjectAnimator.ofFloat(itemView, "alpha", 0.f, 0.5f, 1.0f);
ObjectAnimator.ofFloat(itemView, "alpha", 0.f).start();
animator.setStartDelay(isNotFirstItem ? DURATION / 2 : (i * DURATION / 3));
animator.setDuration(500);
animatorSet.play(animator);
animator.start();
}
public class ViewHolder extends RecyclerView.ViewHolder {
CircleImageView imageView;
TextView name;
View parentLayout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image);
name = itemView.findViewById(R.id.name);
parentLayout = itemView.findViewById(R.id.parentLayout);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment