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
How to Use Different Jsonproperty on Serialize & Deserialize Using Jackson API
How to Loop Through List of Webelements and Select One Webelement With a Condition
How to Delete the Content of Text File Without Deleting Itself
How to Force Java Server to Accept Only Tls 1.2 and Reject Tls 1.0 and Tls 1.1 Connections
How to Retrieve Element Value from Soap Response Using Java
Gson Expected Begin_Array But Was String At Line 1 Column 62
Select the Letters After - in a String
Using Nested While Loop to Print Pyramid of Stars
Java.Sql.Sqlexception: Field Doesn't Have a Default Value
Simplifying the If-Else Condition Using Java Functional Style Programming
Setting Default Values to Null Fields When Mapping With Jackson
How to Avoid Thread.Sleep in Unit Tests
Safely Generate Random Numbers Between Some Range in Java
Cannot Get Jedis Connection; Could Not Get a Resource from the Pool
Java Simpledateformatter With 10 Decimals After the Seconds, Cannot Convert to Date