package com.miris.ui.adapter; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.animation.DecelerateInterpolator; import android.view.animation.Interpolator; import android.widget.FrameLayout; import android.widget.ImageView; import com.miris.R; import com.miris.Utils; import com.squareup.picasso.Callback; import com.squareup.picasso.Picasso; import butterknife.ButterKnife; import butterknife.InjectView; import static com.miris.ui.activity.BaseActivity.userProImgData; /** * Created by Miris on 09.02.15. */ public class UserProfileAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private static final int PHOTO_ANIMATION_DELAY = 600; private static final Interpolator INTERPOLATOR = new DecelerateInterpolator(); private final Context context; private final int cellSize; private int photos; private boolean lockedAnimations = false; private int lastAnimatedItem = -1; public UserProfileAdapter(Context context) { this.context = context; this.cellSize = Utils.getScreenWidth(context) / 3; this.photos = 0; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { final View view = LayoutInflater.from(context).inflate(R.layout.item_photo, parent, false); StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams(); layoutParams.height = cellSize; layoutParams.width = cellSize; layoutParams.setFullSpan(false); view.setLayoutParams(layoutParams); return new PhotoViewHolder(view); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { bindPhoto((PhotoViewHolder) holder, position); } private void bindPhoto(final PhotoViewHolder holder, int position) { Picasso.with(context) .load(userProImgData.get(position).getuser_img_url()) .resize(cellSize, cellSize) .centerCrop() .into(holder.ivPhoto, new Callback() { @Override public void onSuccess() { animatePhoto(holder); } @Override public void onError() { } }); if (lastAnimatedItem < position) lastAnimatedItem = position; } private void animatePhoto(PhotoViewHolder viewHolder) { if (!lockedAnimations) { if (lastAnimatedItem == viewHolder.getPosition()) { setLockedAnimations(true); } long animationDelay = PHOTO_ANIMATION_DELAY + viewHolder.getPosition() * 30; viewHolder.flRoot.setScaleY(0); viewHolder.flRoot.setScaleX(0); viewHolder.flRoot.animate() .scaleY(1) .scaleX(1) .setDuration(200) .setInterpolator(INTERPOLATOR) .setStartDelay(animationDelay) .start(); } } @Override public int getItemCount() { return photos; } static class PhotoViewHolder extends RecyclerView.ViewHolder { @InjectView(R.id.flRoot) FrameLayout flRoot; @InjectView(R.id.ivPhoto) ImageView ivPhoto; public PhotoViewHolder(View view) { super(view); ButterKnife.inject(this, view); } } public void setLockedAnimations(boolean lockedAnimations) { this.lockedAnimations = lockedAnimations; } public void updateItems(boolean animated) { photos = userProImgData.size(); notifyDataSetChanged(); } }