Reputation: 15
Good day! There is a code:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private LayoutInflater inflater;
private List<PojoClassPrc> prc;
Context context;
RecyclerViewAdapter(Context context, List<PojoClassPrc> procedures) {
this.prc = procedures;
this.inflater = LayoutInflater.from(context);
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, int position) {
PojoClassPrc procedures = prc.get(position);
holder.sName1.setText(procedures.getsText1());
holder.sName3.setText(procedures.getsText3());
holder.sName2.setText(procedures.getsText2());
holder.sName1.setTextColor(Color.parseColor("#010101"));
holder.sName2.setTextColor(Color.parseColor("#ACACAC"));
holder.sName3.setTextColor(Color.parseColor("#737373"));
}
@Override
public int getItemCount() {
return prc.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
final TextView sName1, sName2, sName3;
final CheckBox sName5;
final CardView sName4;
ViewHolder(View view) {
super(view);
sName1 = (TextView) view.findViewById(R.id.lblListItem);
sName5 = (CheckBox) view.findViewById(R.id.checkBox2);
sName3 = (TextView) view.findViewById(R.id.lblListItem3);
sName2 = (TextView) view.findViewById(R.id.lblListItem2);
sName4 = (CardView) view.findViewById(R.id.item_card);
final boolean[] clicked = {false};
SharedPreferences prefs = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
boolean cbSelect = prefs.getBoolean("sName", false);
if (cbSelect){
sName3.setTextColor(Color.parseColor("#178DFC"));
sName4.setCardBackgroundColor(Color.parseColor("#C9FDFE"));
} else {
sName3.setTextColor(Color.parseColor("#737373"));
sName4.setCardBackgroundColor(Color.WHITE);
}
sName4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!clicked[0]) {
sName5.setChecked(true);
sName3.setTextColor(Color.parseColor("#178DFC"));
sName4.setCardBackgroundColor(Color.parseColor("#C9FDFE"));
clicked[0] = true;
savePrefs(true, "sName");
} else {
sName5.setChecked(false);
sName3.setTextColor(Color.parseColor("#737373"));
sName4.setCardBackgroundColor(Color.WHITE);
clicked[0] = false;
savePrefs(false, "sName");
}
}});
}
}
private void savePrefs(boolean value, String name) {
SharedPreferences.Editor editor = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE).edit();
editor.putBoolean(name, value);
editor.apply();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
}
sName4 is CardView, the point is that I want to implement such a thing: I clicked on the card, the color of the text changed and the checkbox was set, but in the second press it is necessary to make it as before, say the checkbox is removed, and the color turns white, I can’t realize it in any way with if else, I don’t catch up with what and how, tell me please! Thank you in advance!!!
P.S. I have a lot of fragments, so I do everything in the adapter
Upvotes: 0
Views: 185
Reputation: 858
You can use state of CheckBox by calling method isChecked()
Try this:
sName4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (sName3.isChecked()){
sName3.setChecked(false);
sName3.setTextColor(Color.parseColor("#17fc6b")); // set your color
}else{
sName3.setChecked(true);
sName3.setTextColor(Color.parseColor("#178DFC"));
}
}});
Upvotes: 2