Skip to content

Instantly share code, notes, and snippets.

@Isabellle
Created July 2, 2016 13:51
Show Gist options
  • Save Isabellle/86e182dd56888c545224ead4deb72f6c to your computer and use it in GitHub Desktop.
Save Isabellle/86e182dd56888c545224ead4deb72f6c to your computer and use it in GitHub Desktop.
Simple Recyclerview + Viewholder
package com.yatoo.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import com.getbase.floatingactionbutton.FloatingActionButton;
import com.github.amlcurran.showcaseview.ShowcaseView;
import com.github.amlcurran.showcaseview.targets.Target;
import com.github.amlcurran.showcaseview.targets.ViewTarget;
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.ValueEventListener;
import com.yatoo.BaseActivity;
import com.yatoo.R;
import com.yatoo.adapter.ItemsAdapter;
import com.yatoo.manager.SharedPreferenceManager;
import com.yatoo.model.Item;
import com.yatoo.utils.Constants;
import com.yatoo.utils.Utils;
import java.util.ArrayList;
import java.util.List;
public class ItemListingActivity extends BaseActivity implements View.OnClickListener{
private ItemsAdapter itemsAdapter;
private RecyclerView recyclerView;
private GridLayoutManager gridLayoutManager;
private List<Item> itemList;
public static List<String> itemFavoriteList;
private DatabaseReference mDatabaseReference;
private LinearLayout progressBar;
private FloatingActionButton floatingAddItem;
private String userUID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.activity_item_listing, frameLayout);
userUID = SharedPreferenceManager.getUserUid(this);
progressBar = (LinearLayout)findViewById(R.id.progress_bar);
floatingAddItem = (FloatingActionButton) findViewById(R.id.add_item_call_to_action);
floatingAddItem.setOnClickListener(this);
getFavorites();
Target viewTarget = new ViewTarget(R.id.add_item_call_to_action, this);
new ShowcaseView.Builder(this)
.setTarget(viewTarget)
.setContentTitle(R.string.give_easily_object)
.setContentText(R.string.add_item_tutorial_subtitle)
.setStyle(R.style.CustomShowcaseTheme)
.singleShot(42)
.build();
itemList = new ArrayList<>();
itemFavoriteList = new ArrayList<>();
recyclerView = (RecyclerView)findViewById(R.id.main_recyclerview);
gridLayoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(gridLayoutManager);
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child(Constants.FIREBASE_LOCATION_ITEMS);
ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
getUpdates(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
getUpdates(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
getUpdates(dataSnapshot);
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
Utils.showSnack(rootView, getString(R.string.common_error_occured)+databaseError);
}
};
mDatabaseReference.addChildEventListener(childEventListener);
itemsAdapter = new ItemsAdapter(itemList);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(itemsAdapter);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.add_item_call_to_action:
Intent intent = new Intent(ItemListingActivity.this, WizardAddItem.class);
startActivity(intent);
break;
}
}
/*@Override
public void onClick(int position) {
Item itemSelected = itemArrayList.get(position);
Intent intentItemDetail = new Intent(ItemListingActivity.this, ItemDetailDisplayActivity.class);
intentItemDetail.putExtra("ITEM_SELECTED", itemSelected);
startActivity(intentItemDetail);
}*/
public void getUpdates(DataSnapshot dataSnapshot){
Item item = dataSnapshot.getValue(Item.class);
itemList.add(item);
itemsAdapter.refreshItem(itemList);
progressBar.setVisibility(View.GONE);
}
public void getFavorites(){
DatabaseReference mFirebaseRef = FirebaseDatabase.getInstance().getReference().child("users").child(userUID).child("favorites");
mFirebaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
itemFavoriteList.clear();
if(dataSnapshot.getChildrenCount() > 0){
Log.v(Constants.LOG_TAG, "nb: "+dataSnapshot.getChildrenCount());
for (DataSnapshot data : dataSnapshot.getChildren()) {
itemFavoriteList.add(data.child("uniqueID").getValue().toString());
}
}
Log.v("com.item", String.valueOf(itemFavoriteList));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
package com.yatoo.adapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.firebase.database.DatabaseReference;
import com.like.LikeButton;
import com.like.OnLikeListener;
import com.squareup.picasso.Picasso;
import com.yatoo.R;
import com.yatoo.activity.ItemListingActivity;
import com.yatoo.manager.SharedPreferenceManager;
import com.yatoo.model.Item;
import com.yatoo.utils.Utils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by audaxis on 01/07/16.
*/
public class ItemsAdapter extends RecyclerView.Adapter<ItemsAdapter.ItemViewHolder> {
List<Item> itemArrayList = new ArrayList<>();
public ItemsAdapter(List<Item> itemArrayList){
this.itemArrayList = itemArrayList;
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false);
ItemViewHolder itemViewHolder = new ItemViewHolder(view);
return itemViewHolder;
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
Item item = itemArrayList.get(position);
//We check if the item is in the favoriteArray
for (String s : ItemListingActivity.itemFavoriteList) {
int i = s.indexOf(item.getUniqueID());
if (i >= 0) {
holder.likeButton.setLiked(true);
}
}
holder.itemTitle.setText(item.getTitle());
holder.itemLocation.setText(item.getLocation().getCity());
holder.itemPublicationDate.setText(Utils.getHumanReadableDate(holder.itemView, new Date(item.getPublishedDate())));
if(item.getFrontImage() != null){
Picasso.with(holder.itemView.getContext())
.load(item.getFrontImage())
.into(holder.itemFrontImage);
}
}
public void refreshItem(List<Item> items) {
itemArrayList.clear();
itemArrayList.addAll(items);
this.notifyDataSetChanged();
}
@Override
public int getItemCount() {
return itemArrayList.size();
}
public static class ItemViewHolder extends RecyclerView.ViewHolder implements OnLikeListener{
private TextView itemTitle, itemPublicationDate, itemLocation;
private ImageView itemFrontImage;
private DatabaseReference mFirebaseRef;
private String UserUID;
private 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);
}
@Override
public void liked(LikeButton likeButton) {
}
@Override
public void unLiked(LikeButton likeButton) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment