Last active
July 2, 2016 11:49
-
-
Save Isabellle/7e01e84a4fdf08bd8ff7cd73b373a70c to your computer and use it in GitHub Desktop.
RecyclerView + Viewholder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.yatoo.adapter.recyclerView; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.ViewGroup; | |
/** | |
* Created by student5303 on 4/04/16. | |
*/ | |
public abstract class ClickableRecyclerViewAdapter<VH extends com.yatoo.adapter.recyclerView.ClickableViewHolder> extends RecyclerView.Adapter implements com.yatoo.adapter.recyclerView.ClickableViewHolder.ClickableViewHolderListener { | |
public interface ClickableRecyclerViewAdapterListener { | |
void onClick(int position); | |
} | |
private ClickableRecyclerViewAdapterListener listener; | |
@Override | |
public VH onCreateViewHolder(ViewGroup parent, int viewType) { | |
VH clickableViewHolder = instantiateViewHolder(parent, viewType); | |
clickableViewHolder.setListener(this); | |
return clickableViewHolder; | |
} | |
public void setListener(ClickableRecyclerViewAdapterListener listener) { | |
this.listener = listener; | |
} | |
@Override | |
public final void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { | |
bindCurrentViewHolder((VH)holder, position); | |
} | |
public abstract void bindCurrentViewHolder(VH holder, int position); | |
public abstract VH instantiateViewHolder(ViewGroup parent, int viewType); | |
@Override | |
public void onClick(int position) { | |
if(listener!=null) { | |
listener.onClick(position); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.yatoo.adapter.recyclerView; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.Log; | |
import android.view.View; | |
import com.like.LikeButton; | |
import com.like.OnLikeListener; | |
import com.yatoo.R; | |
/** | |
* Created by student5303 on 4/04/16. | |
*/ | |
public class ClickableViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ | |
public interface ClickableViewHolderListener { | |
void onClick(int position); | |
} | |
private ClickableViewHolderListener listener; | |
public ClickableViewHolder(View itemView) { | |
super(itemView); | |
itemView.setOnClickListener(this); | |
} | |
public void setListener(ClickableViewHolderListener listener) { | |
this.listener = listener; | |
} | |
@Override | |
public void onClick(View view) { | |
if(listener!=null) { | |
listener.onClick(getAdapterPosition()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.yatoo.adapter; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.animation.AlphaAnimation; | |
import android.view.animation.Animation; | |
import android.view.animation.AnimationUtils; | |
import com.yatoo.R; | |
import com.yatoo.adapter.recyclerView.ClickableRecyclerViewAdapter; | |
import com.yatoo.model.Item; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by student5303 on 16/03/16. | |
*/ | |
public class ItemAdapter extends ClickableRecyclerViewAdapter<ItemViewHolder> { | |
private List<Item> listItems; | |
private LayoutInflater layoutInflater; | |
private Context context; | |
private int lastPosition = -1; | |
public ItemAdapter(Context context) { | |
layoutInflater = LayoutInflater.from(context); | |
listItems = new ArrayList<>(); | |
this.context=context; | |
} | |
public void refreshItem(List<Item> items) { | |
listItems.clear(); | |
listItems.addAll(items); | |
this.notifyDataSetChanged(); | |
} | |
@Override | |
public void bindCurrentViewHolder(ItemViewHolder holder, int position) { | |
Item item = listItems.get(position); | |
holder.setItem(item, context); | |
//setAnimation(rowItem, position); | |
// Set the view to fade in | |
//setFadeAnimation(holder.itemView); | |
} | |
@Override | |
public ItemViewHolder instantiateViewHolder(ViewGroup parent, int viewType) { | |
//Similaire à l'adapter d'une ListView | |
View rowItem = layoutInflater.inflate(R.layout.row_item, | |
parent, false); | |
ItemViewHolder itemViewHolder = new ItemViewHolder(rowItem); | |
itemViewHolder.setListener(this); | |
return itemViewHolder; | |
} | |
@Override | |
public int getItemCount() { | |
return listItems.size(); | |
} | |
/** | |
* 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; | |
} | |
} | |
private void setFadeAnimation(View view) { | |
AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f); | |
anim.setDuration(1000); | |
view.startAnimation(anim); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.yatoo.adapter; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.preference.PreferenceManager; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import com.google.firebase.database.ChildEventListener; | |
import com.google.firebase.database.DataSnapshot; | |
import com.google.firebase.database.DatabaseError; | |
import com.google.firebase.database.DatabaseReference; | |
import com.google.firebase.database.FirebaseDatabase; | |
import com.google.firebase.database.Query; | |
import com.google.firebase.database.ValueEventListener; | |
import com.like.LikeButton; | |
import com.like.OnLikeListener; | |
import com.squareup.picasso.Picasso; | |
import com.yatoo.R; | |
import com.yatoo.activity.FavoritesActivity; | |
import com.yatoo.activity.ItemListingActivity; | |
import com.yatoo.adapter.recyclerView.ClickableViewHolder; | |
import com.yatoo.manager.SharedPreferenceManager; | |
import com.yatoo.model.Item; | |
import com.yatoo.model.Location; | |
import com.yatoo.utils.Constants; | |
import com.yatoo.utils.Utils; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.HashMap; | |
public class ItemViewHolder extends ClickableViewHolder implements OnLikeListener{ | |
//private final LikeButton likeButton; | |
private Item item; | |
private TextView itemTitle, itemPublicationDate, itemLocation; | |
private ImageView itemFrontImage; | |
private SharedPreferences sp; | |
private DatabaseReference mFirebaseRef; | |
private Context context; | |
private String UserUID; | |
private static LikeButton likeButton; | |
public ItemViewHolder(View itemView) { | |
super(itemView); | |
itemTitle = (TextView) itemView.findViewById(R.id.row_item_title_textview); | |
itemPublicationDate = (TextView) itemView.findViewById(R.id.row_item_publication_date_textview); | |
itemFrontImage = (ImageView) itemView.findViewById(R.id.row_item_thumbnail_imageView); | |
itemLocation = (TextView) itemView.findViewById(R.id.row_item_location); | |
UserUID = SharedPreferenceManager.getUserUid(itemView.getContext()); | |
likeButton = (LikeButton) itemView.findViewById(R.id.like_button); | |
likeButton.setOnLikeListener(this); | |
} | |
public void setItem(Item item, Context context) { | |
//We check if the item is in the favoriteArray | |
for (String s : ItemListingActivity.itemFavoriteArrayList) { | |
int i = s.indexOf(item.getUniqueID()); | |
if (i >= 0) { | |
likeButton.setLiked(true); | |
} | |
} | |
this.item = item; | |
this.context = context; | |
itemTitle.setText(item.getTitle()); | |
itemLocation.setText(item.getLocation().getCity()); | |
itemPublicationDate.setText(Utils.getHumanReadableDate(itemView, new Date(item.getPublishedDate()))); | |
if(item.getFrontImage() != null){ | |
Picasso.with(context) | |
.load(item.getFrontImage()) | |
.into(itemFrontImage); | |
} | |
} | |
@Override | |
public void liked(LikeButton likeButton) { | |
mFirebaseRef = FirebaseDatabase.getInstance().getReference(); | |
DatabaseReference queryRef = mFirebaseRef.child("users").child(UserUID).child("favorites").child(item.getUniqueID()); | |
queryRef.setValue(item, new DatabaseReference.CompletionListener() { | |
@Override | |
public void onComplete(DatabaseError firebaseError, DatabaseReference firebase) { | |
if (firebaseError != null) { | |
Utils.showSnack(itemView, R.string.error_data_not_saved +" "+firebaseError.getMessage()); | |
} | |
} | |
}); | |
} | |
@Override | |
public void unLiked(LikeButton likeButton) { | |
mFirebaseRef = FirebaseDatabase.getInstance().getReference(); | |
DatabaseReference queryRef = mFirebaseRef.child("users").child(UserUID).child("favorites").child((item.getUniqueID())); | |
queryRef.setValue(null, new DatabaseReference.CompletionListener() { | |
@Override | |
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) { | |
Log.v(Constants.LOG_TAG, "Viewholder: in on complte Unlike"); | |
Utils.showSnack(itemView, context.getString(R.string.item_favorite_removed)); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment