MaskedCarrot
MaskedCarrot

Reputation: 286

Changing color of card in recycle view

I am trying to make an app in android where I need to change the color of the cardview to grey when it is clicked. I have done that by using the code below but I am not able to figure out how to make the color of all other cardview back to white.

, for example, when cardview at index 3 is clicked then it should turn grey buy then when card view at index 5 is clicked then the cardview at index 3 should change its color back to white and only cardview at index 5 should turn grey.

    @NonNull
    @Override
    public Myviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View v = layoutInflater.inflate(R.layout.add_recycle_layout , parent , false);
        return new Myviewholder(v , onCatClick , list.size());
    }

    @Override
    public void onBindViewHolder(@NonNull Myviewholder holder, int position) {
        if(textView == null)
            return;
        textView.setText(list.get(position).getName());
        img.setImageResource(list.get(position).getIcon());
    }

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

    public static class Myviewholder extends RecyclerView.ViewHolder implements View.OnClickListener{

        ImageView img;
        TextView textView;
        onCatClick onCatClick;
        CardView cardView;
        int size;
        public Myviewholder(@NonNull View itemView , onCatClick onCatClick , int size) {
            super(itemView);
            img = itemView.findViewById(R.id.recycle_layout_image);
            textView  = itemView.findViewById(R.id.recycle_layout_text);
            this.onCatClick = onCatClick;
            itemView.setOnClickListener(this);
            cardView = itemView.findViewById(R.id.recycle_layout_card);
            this.size = size;
        }

        @Override
        public void onClick(View v) {
            onCatClick.onCatClicked(getAdapterPosition());
            System.out.println("clicked" + getAdapterPosition());
            cardView.setCardBackgroundColor(Color.parseColor("#e0e0e0"));


        }
    }
    public interface onCatClick{
        void onCatClicked(int position);
    }

Upvotes: 0

Views: 65

Answers (1)

AskNilesh
AskNilesh

Reputation: 69699

You need access your textView and img inside onBindViewHolder() with the help your Myviewholder

Use this

@Override
public void onBindViewHolder(@NonNull Myviewholder holder, int position) {
    
    holder.textView.setText(list.get(position).getName());
    holder.img.setImageResource(list.get(position).getIcon());
}

Instead of this

 @Override
public void onBindViewHolder(@NonNull Myviewholder holder, int position) {
    if(textView == null)
        return;
    textView.setText(list.get(position).getName());
    img.setImageResource(list.get(position).getIcon());
}

try this way to change the color of the selected item

Make one pojo class like this

public class MoodSelection
{
   
    boolean isSelected;
    

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }
}

now use this in your adapter class like this

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.card.MaterialCardView;
import com.moodmedic.R;
import com.moodmedic.activity.ui.mood_fragment.MoodSelection;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

public class SelectMoodAdapter extends RecyclerView.Adapter<SelectMoodAdapter.ViewHolder> {

    private Context mContext;
    private ArrayList<MoodSelection> arrayList = new ArrayList<>();

    public SelectMoodAdapter(Context mContext, ArrayList<MoodSelection> arrayList) {
        this.mContext = mContext;
        this.arrayList = arrayList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.row_select_mood_list_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        if (arrayList.get(position).isSelected()) {
            holder.rootView.setCardBackgroundColor(ContextCompat.getColor(mContext, R.color.colorAccent2));
        } else {
            holder.rootView.setCardBackgroundColor(ContextCompat.getColor(mContext, R.color.colorWhite));

        }

        

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < arrayList.size(); i++) {
                    arrayList.get(i).setSelected(false);
                }
                arrayList.get(position).setSelected(true);
                notifyDataSetChanged();
            }
        });
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        
        MaterialCardView rootView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            
            rootView = itemView.findViewById(R.id.rootView);
        }
    }
}

Upvotes: 1

Related Questions