|
| 1 | +package com.codificador.checkboxrecyclerview; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.text.Editable; |
| 5 | +import android.text.TextWatcher; |
| 6 | +import android.view.LayoutInflater; |
| 7 | +import android.view.View; |
| 8 | +import android.view.ViewGroup; |
| 9 | +import android.widget.CheckBox; |
| 10 | +import android.widget.CompoundButton; |
| 11 | +import android.widget.EditText; |
| 12 | +import android.widget.TextView; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | + |
| 16 | +import androidx.annotation.NonNull; |
| 17 | +import androidx.recyclerview.widget.RecyclerView; |
| 18 | + |
| 19 | +public class Adapter extends RecyclerView.Adapter<Adapter.ItemHolder>{ |
| 20 | + |
| 21 | + ArrayList<Item> items; |
| 22 | + LayoutInflater lInflater; |
| 23 | + |
| 24 | + public Adapter(ArrayList<Item> items, Context context){ |
| 25 | + this.items = items; |
| 26 | + lInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 27 | + } |
| 28 | + |
| 29 | + @NonNull |
| 30 | + @Override |
| 31 | + public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
| 32 | + View view = lInflater.inflate(R.layout.item_row, parent, false); |
| 33 | + ItemHolder itemHolder = new ItemHolder(view); |
| 34 | + return itemHolder; |
| 35 | + } |
| 36 | + |
| 37 | + public ArrayList<Item> getSelectedItems(){ |
| 38 | + ArrayList<Item> result = new ArrayList<>(); |
| 39 | + for(Item item : items){ |
| 40 | + if(item.isSelected()) |
| 41 | + result.add(item); |
| 42 | + } |
| 43 | + return result; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void onBindViewHolder(@NonNull ItemHolder holder, int position) { |
| 48 | + holder.checkBox.setChecked(items.get(position).isSelected()); |
| 49 | + if(items.get(position).getQty() > 0) |
| 50 | + holder.editText.setText(items.get(position).getQty()+""); |
| 51 | + holder.textViewName.setText(items.get(position).getName()); |
| 52 | + holder.checkBox.setTag(position); |
| 53 | + holder.checkBox.setOnCheckedChangeListener(null); |
| 54 | + holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { |
| 55 | + @Override |
| 56 | + public void onCheckedChanged(CompoundButton compoundButton, boolean b) { |
| 57 | + int position = (int) compoundButton.getTag(); |
| 58 | + System.out.println("Changed Pos : "+position+" "+b); |
| 59 | + items.get(position).setSelected(b); |
| 60 | + } |
| 61 | + }); |
| 62 | + CustomTextWatcher customTextWatcher = (CustomTextWatcher) holder.editText.getTag(); |
| 63 | + if(customTextWatcher != null){ |
| 64 | + holder.editText.removeTextChangedListener(customTextWatcher); |
| 65 | + } |
| 66 | + CustomTextWatcher newWatcher = new CustomTextWatcher(position); |
| 67 | + holder.editText.addTextChangedListener(newWatcher); |
| 68 | + } |
| 69 | + |
| 70 | + class CustomTextWatcher implements TextWatcher{ |
| 71 | + |
| 72 | + int position; |
| 73 | + public CustomTextWatcher(int pos){ |
| 74 | + position = pos; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void afterTextChanged(Editable editable) { |
| 89 | + System.out.println(position+" "+editable.toString()); |
| 90 | + if(editable.length() > 0) |
| 91 | + items.get(position).setQty(Integer.parseInt(editable.toString())); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public int getItemCount() { |
| 97 | + return items.size(); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public long getItemId(int position) { |
| 102 | + return position; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public int getItemViewType(int position) { |
| 107 | + return position; |
| 108 | + } |
| 109 | + |
| 110 | + class ItemHolder extends RecyclerView.ViewHolder{ |
| 111 | + TextView textViewName; |
| 112 | + CheckBox checkBox; |
| 113 | + EditText editText; |
| 114 | + |
| 115 | + public ItemHolder(@NonNull View itemView) { |
| 116 | + super(itemView); |
| 117 | + setIsRecyclable(false); |
| 118 | + checkBox = itemView.findViewById(R.id.cb); |
| 119 | + editText = itemView.findViewById(R.id.editText); |
| 120 | + textViewName = itemView.findViewById(R.id.textItem); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments