Sniffer
Sniffer

Reputation: 1595

RecyclerView Cardview item background color change When Click on Its Item

I am trying to change the color (Green)of RecyclerView CardView background when clicks on RecylerView item , when I click on next item of RecyclerView, then previous item must be change / comes to its original color(Pink), and selected item color would be change, that is Green. Can someone give me the proper solution for this.

Plss see image

My Class-:

public class RecylerAdapter extends  RecyclerView.Adapter<RecylerAdapter.ViewHolder>
{   private boolean isSelected;
    private final static int FADE_DURATION = 500;// milliseconds
    private int lastPosition = -1;
    Context cont;
  private  String[] strname;
    private int[] icon;
    public RecylerAdapter(Context con, String[] androidNames, int[] androidIcon)
    {   cont=con;
        strname=androidNames;
        icon=androidIcon;
    }
    class ViewHolder extends RecyclerView.ViewHolder
    {   private ImageView imgView;
        private TextView txtView;
        private CardView cardView;
        private SparseBooleanArray selectedItems = new SparseBooleanArray();

        public ViewHolder(final View itemView)
        {
            super(itemView);

            imgView = (ImageView) itemView.findViewById(R.id.imageView);
            txtView = (TextView) itemView.findViewById(R.id.txt);
            cardView = (CardView) itemView.findViewById(R.id.cv12);


            itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    cardView.isSelected = !cardView.isSelected;
                    notifyDataSetChanged();
                }
            });
        }
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout,parent,false);
        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBindViewHolder(ViewHolder holder, int i)
    {
        if(ViewHolder.isSelected)
        {
            holder.cardView.setBackground(Color.Green);
        }
        else{
            holder.cardView.setBackground(Color.Pink);
        }
        holder.txtView.setText(strname[i]);
        holder.imgView.setImageResource(icon[i]);
        setAnimation(holder.cardView, i);
    }
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setAnimation(View viewToAnimate, int position)
    {
        // If the bound view wasn't previously displayed on screen, it's animated
        if (position > lastPosition)
        {
            //animation 1
            AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
            anim.setDuration(FADE_DURATION);
            viewToAnimate.startAnimation(anim);

            //animation 2
            Animation animation = AnimationUtils.loadAnimation(cont, android.R.anim.slide_in_left);
            viewToAnimate.startAnimation(animation);
            lastPosition = position;
        }
        else
        {
            Animation animation = AnimationUtils.loadAnimation(cont, android.R.anim.slide_in_left);
            viewToAnimate.startAnimation(animation);
            lastPosition = position;
        }
    }
    @Override
    public int getItemCount()
    {
        return strname.length;
    }
    public void setSelected(boolean selection){
        this.isSelected = selection;
    }
    public boolean isSelected(){
        return isSelected;
    }
}

Upvotes: 1

Views: 7143

Answers (2)

Zoraiz Ejaz
Zoraiz Ejaz

Reputation: 167

In your adapter class do this.

public class MyView extends RecyclerView.ViewHolder {

    public TextView textView;
    public ImageView imageView;
    public ImageView lineImageView;

    public MyView(View view) {
        super(view);

        textView = (TextView) view.findViewById(R.id.name);
        imageView = (ImageView) view.findViewById(R.id.food_category_img);
        lineImageView = (ImageView) view.findViewById(R.id.line);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                for(int i=0; i<mDataset.size();i++)
                {
                    mDataset.get(i).setSelected(false);
                }
                mDataset.get(getAdapterPosition()).setSelected(true);
                notifyDataSetChanged();
            }
        });
    }
}

In Bind view holder do this

   if(mDataset.get(position).isSelected()){
        itemView.Background(set color)
    }else{
       itemView.Background(set color)
    }

Upvotes: 0

Ready Android
Ready Android

Reputation: 3622

It's all about your item selection manage using model class:

MyModel.class: This is the class which you are using for showing list of data to recycler view. Add a boolean variable to make selection and deselection.

private boolean isSelected;

public void setSelected(boolean selection){
this.isSelected = selection;
}

public boolean isSelected(){
return isSelected;
}

Now on Item click of recycler view in your adapter:

myModel = list.get(position);
myModel.isSelected = !myModel.isSelected;
notifyDataSetChanged();

In onBindViewHolder method of adapter

    myModel = list.get(position);
    if(myModel.isSelected){
       itemView.setBackground(Color.Green);
    }else{
       itemView.setBackground(Color.Pink);
    }

Use this logic and check, if you found any difficulty let me know.

Your updated code as you are not using list of model class so you can't manage with Model variable selection, check with below:

    public class RecylerAdapter extends RecyclerView.Adapter<RecylerAdapter.ViewHolder> {
    private boolean isSelected;
    private final static int FADE_DURATION = 500;// milliseconds
    private int lastPosition = -1;
    Context cont;
    private String[] strname;
    private int[] icon;
    private int selectedPosition = -1;

    public RecylerAdapter(Context con, String[] androidNames, int[] androidIcon) {
        cont = con;
        strname = androidNames;
        icon = androidIcon;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView imgView;
        private TextView txtView;
        private CardView cardView;
        private SparseBooleanArray selectedItems = new SparseBooleanArray();

        public ViewHolder(final View itemView) {
            super(itemView);
            imgView = (ImageView) itemView.findViewById(R.id.imageView);
            txtView = (TextView) itemView.findViewById(R.id.txt);
            cardView = (CardView) itemView.findViewById(R.id.cv12);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectedPosition = getAdapterPosition();
                    notifyDataSetChanged();
                }
            });
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBindViewHolder(ViewHolder holder, int i) {
        if (selectedPosition == i) {
            holder.cardView.setBackground(Color.Green);
        } else {
            holder.cardView.setBackground(Color.Pink);
        }
        holder.txtView.setText(strname[i]);
        holder.imgView.setImageResource(icon[i]);
        setAnimation(holder.cardView, i);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setAnimation(View viewToAnimate, int position) {
        // If the bound view wasn't previously displayed on screen, it's animated
        if (position > lastPosition) {
            //animation 1
            AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
            anim.setDuration(FADE_DURATION);
            viewToAnimate.startAnimation(anim);

            //animation 2
            Animation animation = AnimationUtils.loadAnimation(cont, android.R.anim.slide_in_left);
            viewToAnimate.startAnimation(animation);
            lastPosition = position;
        } else {
            Animation animation = AnimationUtils.loadAnimation(cont, android.R.anim.slide_in_left);
            viewToAnimate.startAnimation(animation);
            lastPosition = position;
        }
    }

    @Override
    public int getItemCount() {
        return strname.length;
    }
}

Upvotes: 7

Related Questions