Skip to content

Instantly share code, notes, and snippets.

@amit-bhandari
Last active April 14, 2019 14:59
Show Gist options
  • Save amit-bhandari/08913929b720ef01dad916dd84a2c451 to your computer and use it in GitHub Desktop.
Save amit-bhandari/08913929b720ef01dad916dd84a2c451 to your computer and use it in GitHub Desktop.
public class AdapterQuotes extends RecyclerView.Adapter<AdapterQuotes.MyViewHolder>{
private LayoutInflater inflater;
private List<Quote> quoteItems;
private Context context;
//private Typeface type;
public AdapterQuotes(Context context){
this.quoteItems=new ArrayList<>();
inflater= LayoutInflater.from(context);
this.context = context;
//type = Typeface.createFromAsset(context.getAssets(),"fonts/angelina.TTF");
}
@Override
public AdapterQuotes.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_quote, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(AdapterQuotes.MyViewHolder holder, int position) {
holder.quote.setText(quoteItems.get(position).getQuote());
holder.author.setText(quoteItems.get(position).getAuthor());
//just a fancy animation, nothing else
setFadeAnimation(holder.itemView);
}
private void setFadeAnimation(View view) {
ScaleAnimation anim = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(300);
view.startAnimation(anim);
}
@Override
public int getItemCount() {
return quoteItems.size();
}
public void insertItems(List<Quote> quotes){
//add items at the top
for(int i=0;i<quotes.size();i++){
quoteItems.add(0,quotes.get(i));
}
notifyItemRangeInserted(0,quotes.size()-1);
}
public void clearItems(){
quoteItems.clear();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView quote, author;
MyViewHolder(View itemView) {
super(itemView);
quote = (TextView)itemView.findViewById(R.id.quote);
author = (TextView)itemView.findViewById(R.id.author);
// quote.setTypeface(type);
//author.setTypeface(type);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment