Checkbox listener en RecyclerView con DataBinding
Im tratando de rellenar una lista de nota / mensaje con casilla de verificación en la vista del artículo.
He intentado escuchar a la casilla de verificación. Tristemente en la casilla de verificación haga clic en nada sucede.
- android RecyclerView setOnScrollChangeListener necesita api 23?
- No se pudo resolver: com.android.support:cardview-v7:26.0.0 android
- Android: No se puede resolver el método 'findFirstVisibleItemPosition ()'?
- No hay animación en el elemento RecyclerView seleccione Android
- Subclasificación de SimpleCursorAdapter para incluir convertView para la conservación de la memoria
Si fijo el oyente a la opinión del padre. Es capaz de activar el método onClick. Pero esto se activará cada vez que el usuario haga clic en el elemento completo de la lista.
Mis objetivos son más en el establecimiento de escucha para la casilla de verificación.
Tan malo saber que el usuario ha seleccionado nota (s) de la lista.
Heres mi clase del adaptador de código junto con la clase interna del sostenedor de la visión
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>
- GapWorker con vistas desechadas o adjuntas no se puede reciclar. IsScrap: false isAttached: true
- Convertir la altura de RecyclerView en "wrap_content" en el diseño de restricción
- Drawable vs Single reutilizable Bitmap mejor con la memoria?
- Ejemplo sencillo de Android RecyclerView
- Android RecyclerView, el reciclaje no funciona correctamente
- Imágenes en RecyclerView
- Cómo implementar load más recyclerview en android
- Adaptador RecyclerView que toma valores erróneos
¿Qué es NoteListitemBinding? En general, el método onClick debe activarse al hacer clic en la casilla de verificación, al hacer clic en la vista, el grupo de vistas recibirá primero el evento táctil y luego pasará el evento a su hijo, pero si el grupo de vistas previene el evento, se consumirá y El niño no puede obtener el evento, su método onClick no se activará.
Gracias a @ ljl5241861 por mencionar en DataBinding, me di cuenta de que pequeño error tonto en mi código .. con el fin de obtener la opinión del niño responde onClick en necesidad de obtener la vista de databinding.getRoot () no la recopiladora de una vista específica del grupo de artículos. Después de cambiar el constructor de ViewHolder, consiguiendo la opinión del artículo vía el binding.getRoot, i capaz de accionar el oyente del tecleo. ¡Esperanza esto ayuda a otros también !!
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); }
- Android Studio: falló al encontrar el destino de google apis: 8
- Colocar un anuncio de banner para que se sitúe a continuación y se separe de ViewPager