【发布时间】:2016-02-03 01:51:56
【问题描述】:
我正在尝试使用项目视图中的复选框填充注释/消息列表。
我已尝试将侦听器设置为复选框。遗憾的是,单击复选框后没有任何反应。
如果我将监听器设置为父视图。它能够触发 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>
【问题讨论】:
-
请在此处粘贴您的 note_listitem 布局。
标签: android checkbox android-recyclerview