I have a RecycleView Which shows a bunch of list in which one item is selected by showing its background color blue, now I want user to select any item from list and its color get changed to blue how to implement this inside RecyclerView.Adapter or any other logic
public class ToggleAdapter extends RecyclerView.Adapter<ToggleAdapter.ToggleViewHolder>{
private ArrayList<ToggleParams> dataList=new ArrayList<>();
private Context context;
private static int selection;
public ToggleAdapter(ArrayList<ToggleParams> dataList, Context context,int selection) {
setData(dataList,context,selection);
}
@Override
public ToggleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_row,parent,false);
ToggleViewHolder toggleViewHolder=new ToggleViewHolder(v);
return toggleViewHolder;
}
@Override
public void onBindViewHolder(ToggleViewHolder holder, int position) {
if(position==selection){
holder.selected_item.setBackgroundColor(context.getResources().getColor(R.color.blue));
holder.text_view.setTextColor(context.getResources().getColor(android.R.color.white));
}
holder.image_view.setImageDrawable(context.getResources().getDrawable(dataList.get(position).getIMAGE_ID()));
holder.text_view.setText(dataList.get(position).getTOGGLE_TEXT());
}
private void setData(ArrayList<ToggleParams> dataList, Context context,int selection) {
this.dataList = dataList;
this.context = context;
this.selection = selection;
}
@Override
public int getItemCount() {
return dataList.size();
}
public static class ToggleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public LinearLayout selected_item;
public ImageView image_view;
public TextView text_view;
public ToggleViewHolder(View itemView) {
super(itemView);
selected_item= (LinearLayout) itemView.findViewById(R.id.selected_item);
selected_item.setOnClickListener(this);
image_view= (ImageView) itemView.findViewById(R.id.imageView);
text_view= (TextView) itemView.findViewById(R.id.textView);
}
public void onClick(View v) {
selection=getPosition();
//After getting this position I want that this item list in recyclerview to change its background color but how to call notifyDataSetChange() here something equivalent to that
}
}
}