Permalink
Browse files

added likes counter to feed item

  • Loading branch information...
frogermcs committed Jan 6, 2015
1 parent 21f234d commit 090cf15ac0a00ef9bbc5f952fd9c9339838a580f
@@ -1,13 +1,25 @@
package io.github.froger.instamaterial;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextSwitcher;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import butterknife.ButterKnife;
import butterknife.InjectView;
@@ -23,6 +35,7 @@
private int itemsCount = 0;
private boolean animateItems = false;
private final Map<Integer, Integer> likesCount = new HashMap<>();
private OnFeedItemClickListener onFeedItemClickListener;
public FeedAdapter(Context context) {
@@ -62,6 +75,7 @@ public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
holder.ivFeedCenter.setImageResource(R.drawable.img_feed_center_2);
holder.ivFeedBottom.setImageResource(R.drawable.img_feed_bottom_2);
}
updateLikesCounter(holder, false);
holder.btnComments.setOnClickListener(this);
holder.btnComments.setTag(position);
@@ -74,6 +88,21 @@ public int getItemCount() {
return itemsCount;
}
private void updateLikesCounter(CellFeedViewHolder holder, boolean animated) {
int currentLikesCount = likesCount.get(holder.getPosition()) + 1;
String likesCountText = context.getResources().getQuantityString(
R.plurals.likes_count, currentLikesCount, currentLikesCount
);
if (animated) {
holder.tsLikesCounter.setText(likesCountText);
} else {
holder.tsLikesCounter.setCurrentText(likesCountText);
}
likesCount.put(holder.getPosition(), currentLikesCount);
}
@Override
public void onClick(View v) {
final int viewId = v.getId();
@@ -91,16 +120,23 @@ public void onClick(View v) {
public void updateItems(boolean animated) {
itemsCount = 10;
animateItems = animated;
fillLikesWithRandomValues();
notifyDataSetChanged();
}
private void fillLikesWithRandomValues() {
for (int i = 0; i < getItemCount(); i++) {
likesCount.put(i, new Random().nextInt(100));
}
}
public void setOnFeedItemClickListener(OnFeedItemClickListener onFeedItemClickListener) {
this.onFeedItemClickListener = onFeedItemClickListener;
}
public static class CellFeedViewHolder extends RecyclerView.ViewHolder {
@InjectView(R.id.ivFeedCenter)
SquaredImageView ivFeedCenter;
ImageView ivFeedCenter;
@InjectView(R.id.ivFeedBottom)
ImageView ivFeedBottom;
@InjectView(R.id.btnComments)
@@ -109,6 +145,12 @@ public void setOnFeedItemClickListener(OnFeedItemClickListener onFeedItemClickLi
ImageButton btnLike;
@InjectView(R.id.btnMore)
ImageButton btnMore;
@InjectView(R.id.vBgLike)
View vBgLike;
@InjectView(R.id.ivLike)
ImageView ivLike;
@InjectView(R.id.tsLikesCounter)
TextSwitcher tsLikesCounter;
public CellFeedViewHolder(View view) {
super(view);
@@ -118,6 +160,7 @@ public CellFeedViewHolder(View view) {
public interface OnFeedItemClickListener {
public void onCommentsClick(View v, int position);
public void onMoreClick(View v, int position);
}
}
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="150"
android:interpolator="@android:anim/linear_interpolator">
<translate
android:fromYDelta="80%p"
android:toYDelta="0" />
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="150"
android:interpolator="@android:anim/linear_interpolator">
<translate
android:fromYDelta="0"
android:toYDelta="-80%p" />
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
@@ -55,6 +55,39 @@
android:layout_height="48dp"
android:background="@drawable/btn_feed_action"
android:src="@drawable/ic_more_grey" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|right">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_heart_small_blue" />
<TextSwitcher
android:id="@+id/tsLikesCounter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:inAnimation="@anim/slide_in_likes_counter"
android:outAnimation="@anim/slide_out_likes_counter">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123 likes"
android:textColor="@color/text_like_counter" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/text_like_counter" />
</TextSwitcher>
</LinearLayout>
</LinearLayout>
</LinearLayout>
@@ -17,5 +17,7 @@
<color name="btn_context_menu_pressed">#40000000</color>
<color name="btn_context_menu_text_red">#ffff0000</color>
<color name="text_like_counter">#2b5a83</color>
<color name="bg_comments">#ffffff</color>
</resources>
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">InstaMaterial</string>
<plurals name="likes_count">
<item quantity="one">%d like</item>
<item quantity="other">%d likes</item>
</plurals>
</resources>

0 comments on commit 090cf15

Please sign in to comment.