Changing background color of selected item in recyclerview

hey here is the solution to changing the background color of a selected list item in the recycler view list. it’s very easy your can just follow the step for Changing background color of selected item in recyclerview.

******* Change in onBindViewHolder *******

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

holder.linearlayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
index=position;
notifyDataSetChanged();
}
});
if(index==position){
holder.linearlayout.setBackgroundColor(Color.parseColor("#FFEB3B"));
holder.tv.setTextColor(Color.parseColor("#ffffff"));
}
else
{
holder.linearlayout.setBackgroundColor(Color.parseColor("#ffffff"));
holder.tv.setTextColor(Color.parseColor("#6200EA"));
}
}

here ‘row_index’ is set as ‘-1’ initially

public class ViewHolder extends RecyclerView.ViewHolder {
private TextView tv;
LinearLayout linearlayout;
RecyclerView rv;

public ViewHolder(final View itemView) {
super(itemView);
tv=(TextView)itemView.findViewById(R.id.txtView1);
linearlayout=(LinearLayout)itemView.findViewById(R.id.inrLayout);
rv=(RecyclerView)itemView.findViewById(R.id.recyclerView);
}
}
Facebooktwitterredditpinterestlinkedinmail
Recommended
Android How to check user permission is allow or not…