RecyclerView - Duplicate when scrolling, when item touched, it changes the item's background
<p>I have a recyclerview. And I want to change the background of an item when it is clicked.
With my code the clicked item is changed, but when scrolling, there are also items whose background also changed, and when scrolling back, the inital item's background changed back to default.</p>

<p>Here is my adapter:</p>

<blockquote>
  <p>public class MyCustomAdapter extends RecyclerView.Adapter {</p>
</blockquote>

<pre><code>private Context context;

private ArrayList&lt;Information&gt; data;

private LayoutInflater inflater;

private int previousPosition = 0;

private Resources resources;

private Drawable drawable;

public MyCustomAdapter(Context context, ArrayList&lt;Information&gt; data) {

    this.context = context;
    this.data = data;
    inflater = LayoutInflater.from(context);
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int position) {

    View view = inflater.inflate(R.layout.list_item_row, parent, false);

    MyViewHolder holder = new MyViewHolder(view);

    return holder;
}

@Override
public void onBindViewHolder(final MyViewHolder myViewHolder, final int position) {

    myViewHolder.textView1.setText(data.get(position).title);
    //myViewHolder.imageView.setImageResource(data.get(position).imageId);

    if(position &gt; previousPosition){ // We are scrolling DOWN

        AnimationUtil.animate(myViewHolder, true);

    }else{ // We are scrolling UP

        AnimationUtil.animate(myViewHolder, false);
    }

    previousPosition = position;

    final int currentPosition = position;
    final Information infoData = data.get(position);


    myViewHolder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(context, "Onlick Called at position "+position, Toast.LENGTH_LONG).show();
            //myViewHolder.imageView.setImageResource(data.get(position).imageId);
            resources = context.getResources();
            drawable = resources.getDrawable(data.get(position).imageId);
            myViewHolder.relativeLayout.setBackground(drawable);
        }
    });

}

@Override
public int getItemCount() {
    return data.size();
}

class MyViewHolder extends RecyclerView.ViewHolder{

    TextView textView1;
    TextView textView2;
    CardView cardView;
    ImageView imageView;
    RelativeLayout relativeLayout;


    public MyViewHolder(View itemView) {
        super(itemView);
        textView1 = (TextView) itemView.findViewById(R.id.card_front_text1);
        textView2 = (TextView) itemView.findViewById(R.id.card_front_text2);
        imageView = (ImageView) itemView.findViewById(R.id.card_front_imageview);
        relativeLayout = (RelativeLayout) itemView.findViewById(R.id.card_front_relativelayout);
        cardView = (CardView)   itemView.findViewById(R.id.cardView);
    }
}
</code></pre>

<p>}</p>

<p>XML:</p>

<p>Tha parent layout is the Cardview, and there is a relative layout child.</p>