How to Change Background Colour to Specific Viewholder Items in a Recycleview

Changing background color of selected item in recyclerview

Finally, I got the answer.

public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.tv1.setText(android_versionnames[position]);

holder.row_linearlayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
row_index=position;
notifyDataSetChanged();
}
});
if(row_index==position){
holder.row_linearlayout.setBackgroundColor(Color.parseColor("#567845"));
holder.tv1.setTextColor(Color.parseColor("#ffffff"));
}
else
{
holder.row_linearlayout.setBackgroundColor(Color.parseColor("#ffffff"));
holder.tv1.setTextColor(Color.parseColor("#000000"));
}

}

here 'row_index' is set as '-1' initially

public class ViewHolder extends RecyclerView.ViewHolder {
private TextView tv1;
LinearLayout row_linearlayout;
RecyclerView rv2;

public ViewHolder(final View itemView) {
super(itemView);
tv1=(TextView)itemView.findViewById(R.id.txtView1);
row_linearlayout=(LinearLayout)itemView.findViewById(R.id.row_linrLayout);
rv2=(RecyclerView)itemView.findViewById(R.id.recyclerView1);
}
}

Set background color for RecyclerView item depending on content

Is this the problem by any chance?

 if (actualInt != expectedInt) {
holder.countedquantity.setTextColor(Color.RED);
holder.actualquantity.setTextColor(Color.RED);
} else {
holder.countedquantity.setTextColor(Color.RED);
holder.actualquantity.setTextColor(Color.RED);
}

Change to:

if (actualInt != expectedInt) {
holder.countedquantity.setTextColor(Color.RED);
holder.actualquantity.setTextColor(Color.RED);
} else {
holder.countedquantity.setTextColor(Color.BLACK);
holder.actualquantity.setTextColor(Color.BLACK);
}

How to change the background color of selected item in RecyclerView

Call notifyDataSetChanged

holder.itemView.setOnClickListener { 
rowindex = position
mListener?.onItemClick(holder.itemView, categories[position])
notifyDataSetChanged()
}

if (rowindex == position) {
holder.itemView.setBackgroundColor(Color.parseColor("#FED07A"))
} else {
holder.itemView.setBackgroundColor(Color.parseColor("#ffffff"))
}

Set recyclerview item background color repeatedly

It will not work like that. You need to put this logic inside onBindViewHolder where you can change color with help of position.
Use like

public String[] mColors = {"#3F51B5","#FF9800","#009688","#673AB7"};

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
holder.itemView.setBackgroundColor(Color.parseColor(mColors[position % 4])); // 4 can be replaced by mColors.length
}

And look how I redefined mColors array with # prefixed. So you need to add # while calculating colors.

Change background color of selected item in recyclerview

in your model,
take a boolean variable, and create getter setter methods

private boolean isSelected = false

public boolean isSelected() {
return isSelected;
}

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

in your OnBindViewHolder

viewHolder.MainLinearLayout.SetBackgroundColor(list[position].isSelected()?v.Resources.GetColor(Resource.Color.white):v.Resources.GetColor(Resource.Color.black));

in your onClick event, // Code below is edited

Note: For Best practice setTag(position) with your position to your view that is getting clicked and use that tag value as a position.

int pos = (int) view.getTag(); 
list[pos].setSelected(!list[pos].isSelected());
notifyItemChanged(position);


Related Topics



Leave a reply



Submit