简体   繁体   中英

CardView onClick click test?

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!!!

PS I have a lot of fragments, so I do everything in the adapter

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"));
      }
}});

more about checkbox

insert a boolean checker like

boolean clicked = false;
Context context; //get context from Constructor 

SharedPreferences prefs = getSharedPreferences("myPrefs", Context.MODE_PRIVATE); //if creating in Activity remove Context
boolean cbSelect = prefs.getBoolean("sName", false); //false is default value, if sName is null or not found it will return false

if (cbSelect){
   //your checkBox was selected change color
} else {
   //your checkBox was not selected, do not change color
}

sName4.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
   if(!checked){
     sName3.setChecked(true);
     sName3.setTextColor(Color.parseColor("#178DFC"));
     checked = true;
     savePrefs(true, "sName");
   } else {
     //your default state
     sName3.setChecked(false);
     sName3.setTextColor(Color.parseColor("#FFFFFF"));
     checked = false;
     savePrefs(false, "sName")
   }
 }
}); 

Create a Method in your adapter

private void savePrefs(boolean value, String name){
   //here name represent the name of your checkBox
   SharedPreferences.Editor editor = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE).edit(); //if creating in Activity remove Context
   editor.putBoolean(name, value);
   editor.apply();
}

This will give you desired result hope this will help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM