Skip to content

Instantly share code, notes, and snippets.

@mislavs
Created October 29, 2014 19:58
Show Gist options
  • Save mislavs/aa7b1ece00c566a9b09a to your computer and use it in GitHub Desktop.
Save mislavs/aa7b1ece00c566a9b09a to your computer and use it in GitHub Desktop.
RecyclerView.Adapter implementation for displaying a list of Book objects.
package com.msvs.bookshelf.adapters;
import android.os.Handler;
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.msvs.bookshelf.R;
import com.msvs.bookshelf.events.BookSelectedEvent;
import com.msvs.bookshelf.model.entities.Book;
import com.squareup.picasso.Picasso;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.InjectView;
import de.greenrobot.event.EventBus;
/**
* An adapter for displaying a list of {@link com.msvs.bookshelf.model.entities.Book} objects.
*/
public class BooksAdapter extends RecyclerView.Adapter<BooksAdapter.ViewHolder> {
private final int NOTIFY_DELAY = 500;
private List<Book> mBooks;
public static class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
@InjectView(R.id.book_list_item_title) TextView mTitle;
@InjectView(R.id.book_list_item_authors) TextView mAuthors;
@InjectView(R.id.book_list_item_cover) ImageView mCover;
public ViewHolder(View view) {
super(view);
ButterKnife.inject(this, view);
view.setOnClickListener(this);
}
@Override
public void onClick(View view){
EventBus.getDefault().post(new BookSelectedEvent(
(Long)view.getTag(R.string.tag_book_id),
(String)view.getTag(R.string.tag_book_title)));
}
}
public BooksAdapter(List<Book> books) {
mBooks = books;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater
.from(parent.getContext())
.inflate(R.layout.list_item_book, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
Book book = mBooks.get(position);
Picasso.with(viewHolder.mCover.getContext())
.load(book.getCoverImagePath())
.resize(500, 500)
.centerInside()
.into(viewHolder.mCover);
viewHolder.mTitle.setText(book.getTitle());
viewHolder.mAuthors.setText(book.getAuthorsConcat());
viewHolder.itemView.setTag(R.string.tag_book_id, book.getId());
viewHolder.itemView.setTag(R.string.tag_book_title, book.getTitle());
}
@Override
public long getItemId(int position) {
return mBooks.get(position).getId();
}
@Override
public int getItemCount() {
return mBooks.size();
}
// region List manipulation methods
public void addBook(final Book book, final int position) {
// notify of the insertion with a delay, so there is a brief pause after returning
// from the new book screen; this makes the animation more noticeable
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mBooks.add(position, book);
notifyItemInserted(position);
}
}, NOTIFY_DELAY);
}
public void removeBook(final int position) {
mBooks.remove(position);
// notify of the removal with a delay so there is a brief pause after returning
// from the book details screen; this makes the animation more noticeable
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
notifyItemRemoved(position);
}
}, NOTIFY_DELAY);
}
// endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment