Skip to content

Instantly share code, notes, and snippets.

@dimrsilva
Last active May 23, 2016 17:42
Show Gist options
  • Save dimrsilva/9792af16c0f87d4ad049329568c064fd to your computer and use it in GitHub Desktop.
Save dimrsilva/9792af16c0f87d4ad049329568c064fd to your computer and use it in GitHub Desktop.
package com.example;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
/**
* Created by andersonr on 23/05/16.
*/
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.SimpleViewHolder> {
private final Context context;
private final Drawable drawableOdd;
private final Drawable drawableEven;
public ExampleAdapter(Context context) {
this.context = context;
this.drawableOdd = DrawableCompat.wrap(ContextCompat.getDrawable(context, R.drawable.ic_account));
DrawableCompat.setTint(this.drawableOdd, ContextCompat.getColor(context, R.color.blue));
this.drawableEven = DrawableCompat.wrap(ContextCompat.getDrawable(context, R.drawable.ic_account));
DrawableCompat.setTint(this.drawableEven, ContextCompat.getColor(context, R.color.red));
}
@Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_example, parent, false);
return new SimpleViewHolder(view);
}
@Override
public void onBindViewHolder(SimpleViewHolder holder, int position) {
if (position % 2 == 0) {
holder.imageView.setImageDrawable(drawableOdd);
} else {
holder.imageView.setImageDrawable(drawableEven);
}
}
@Override
public int getItemCount() {
return 0;
}
static class SimpleViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
public SimpleViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.image);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment