【问题标题】:Check Box listener on RecyclerView with DataBinding带有 DataBinding 的 RecyclerView 上的复选框侦听器
【发布时间】: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


【解决方案1】:

什么是 NoteListitemBinding ?一般来说,onClick方法必须在单击复选框时触发,当您单击视图时,视图组将首先接收触摸事件,然后将事件传递给其子级,但如果视图组阻止该事件,则该事件将被消耗,并且child 无法获取事件,不会触发其 onClick 方法。

【讨论】:

  • @Ijl5241861 我正在使用 DataBinding 将我的数据绑定到视图持有者。
【解决方案2】:

感谢@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);
    }

【讨论】:

    猜你喜欢
    • 2012-11-14
    • 2012-10-30
    • 1970-01-01
    • 2014-03-06
    • 2011-06-30
    • 2015-10-20
    • 1970-01-01
    • 2018-12-26
    相关资源
    最近更新 更多