我试图在项目视图中使用复选框填充注释/消息列表。
我已尝试将侦听器设置为复选框。遗憾的是,在复选框上点击没有任何反应。
如果我将侦听器设置为父视图。它能够触发onClick方法。但每次用户点击列表中的整个项目时都会触发此操作。
我的目标更多是关于为复选框设置监听器。
很难知道用户已经从列表中选择了一个音符。
继承我的代码适配器类以及视图持有者内部类
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>
答案 0 :(得分:1)
什么是NoteListitemBinding?一般来说,单击复选框时必须触发onClick方法,当您单击视图时,视图组将首先接收触摸事件,然后将事件传递给其子视图,但如果视图组阻止该事件,则事件将被消耗,并且child无法获取事件,其onClick方法不会被触发。
答案 1 :(得分:0)
感谢@ljl5241861提到DataBinding,我意识到我的代码中有一个小傻错...为了让子视图响应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);
}