Check Box receiver on RecyclerView with DataBinding

im trying to populate a list of notes / posts with a checkbox in the item view.

I tried installing a listener. sad when you select the checkbox nothing happens.

if I set the listener to the parent view. its way to run the onClick method. but this will fire every time the user clicks on the entire item in the list.

my goals are more about setting the receiver for the checkbox.

you know so poorly that the user selected notes from the list.

heres my code adapter class along with the inner class of the view owner

public class BroadcastRVA extends RecyclerView.Adapter<BroadcastRVA.BroadcastVH>{

private Context mContext;
private ObservableArrayList<MNote> notes;
private LayoutInflater inflater;

public BroadcastRVA(Context mContext, ObservableArrayList<MNote> notes, LayoutInflater inflater) {
    this.mContext = mContext;
    this.notes = notes;
    this.inflater = LayoutInflater.from(mContext);
}

@Override
public BroadcastRVA.BroadcastVH onCreateViewHolder(ViewGroup parent, int viewType) {
    NoteListitemBinding binding = NoteListitemBinding.inflate(inflater);
    View view = LayoutInflater.from(parent.getContext()).inflate(
            R.layout.note_listitem, null);
    BroadcastVH viewHolder = new BroadcastVH(binding, view);
    // create a new view
    return viewHolder;
}

@Override
public void onBindViewHolder(BroadcastVH holder, final int position) {
    final  MNote note = notes.get(position);
    holder.cbox.setChecked(note.isSelected());
    holder.cbox.setTag(note);
    holder.vBinding.setNote(note);
    holder.cbox.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            CheckBox cb = (CheckBox) v;
            MNote note = (MNote) cb.getTag();
            note.setIsSelected(cb.isChecked());
            notes.get(position).setIsSelected(cb.isChecked());
            Toast.makeText(
                    v.getContext(),
                    "Clicked on Checkbox: " + cb.getText() + " is "
                            + cb.isChecked(), Toast.LENGTH_LONG).show();
        }
    });
}

public ObservableArrayList<MNote> getNotes() {
    return notes;
}
/**
 * Returns the total number of items in the data set hold by the adapter.
 *
 * @return The total number of items in this adapter.
 */
@Override
public int getItemCount() {
    if (notes != null)
        return notes.size();
    else return 0;
}

public class BroadcastVH extends RecyclerView.ViewHolder {
    NoteListitemBinding vBinding;
    TextView uuid;
    CheckBox cbox;

    public BroadcastVH(NoteListitemBinding binding, View view) {
        super(binding.getRoot());
        this.vBinding = binding;
        this.uuid = (TextView) view.findViewById(R.id._UUID);
        this.cbox = (CheckBox) view.findViewById(R.id.deleteNote);
    }
    }
} 

note_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="note"
            type="com.pbasolutions.android.model.MNote" />
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="1">
        <TableLayout android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                >
                <CheckBox
                    android:layout_width="30dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/deleteNote"
                    android:clickable="true"/>
                <TextView
                    android:layout_width="270dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/textViewNote"
                    android:layout_column="1"
                    android:layout_marginLeft="5dp"
                    android:text="@{note.textMsgs}"
                    android:editable="false"
                    android:textSize="22sp"/>
                <TableLayout android:layout_column="1">
                    <TableRow>
                        <TextView
                            android:layout_width="80dp"
                            android:layout_height="wrap_content"
                            android:id="@+id/textViewNoteDate"
                            android:layout_column="0"
                            android:text="@{note.date}"
                            android:editable="false"
                            android:textSize="15sp"/>
                    </TableRow>
                </TableLayout>
                <TextView
                    android:layout_width="270dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/_UUID"
                    android:layout_marginLeft="5dp"
                    android:text="@{note._UUID}"
                    android:visibility="invisible"/>
            </TableRow>
        </TableLayout>
        <View style="@style/Line" />
    </LinearLayout>
</layout>
+4
source share
2 answers

NoteListitemBinding? , onClick , , , , , , , onClick .

+1

@ljl5241861 DataBinding, , . child onClick, databinding.getRoot(), . ViewHolder, binding.getRoot, .. , !

public BroadcastVH(NoteListitemBinding binding, View view) {
        super(binding.getRoot());
        this.vBinding = binding;
        View bindView = binding.getRoot(); 
        this.uuid = (TextView) bindView.findViewById(R.id._UUID);
        this.cbox = (CheckBox) bindView.findViewById(R.id.deleteNote);
    }
0

Source: https://habr.com/ru/post/1614211/


All Articles