You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android RecyclerView条目点击无响应求助(已实现监听接口)

RecyclerView自定义点击接口无响应问题排查与修复

核心问题:未给条目视图绑定点击监听

你的ViewHolder类虽然实现了View.OnClickListener接口,但没有将条目根视图(itemView)的点击事件绑定到当前ViewHolder实例,导致点击事件根本不会触发onClick方法。

修复步骤

在ManageAttendanceRecyclerViewAdapter的ViewHolder构造函数中,添加点击监听绑定代码:

public ViewHolder(@NonNull View itemView, Context ctx) {
    super(itemView);

    this.onAttendanceClickListener = ManageAttendanceRecyclerViewAdapter.this.onAttendanceClickListener;

    context = ctx;
    nameTextView = itemView.findViewById(R.id.manage_attendance_name_textview);
    confirmation = itemView.findViewById(R.id.manage_attendance_confirmation);
    remarkOrReasonTextView = itemView.findViewById(R.id.manage_attendance_remark_reason_textview);
    
    // 新增:绑定点击监听
    itemView.setOnClickListener(this);
}

额外优化建议

  1. 字符串空值判断优化
    原代码中直接用==比较字符串为空的写法存在空指针风险,建议替换为TextUtils.isEmpty():
// 替换前
if (confirmation.getRemarkOrReason() == "") {
    holder.remarkOrReasonTextView.setText("null");
}
// 替换后
if (TextUtils.isEmpty(confirmation.getRemarkOrReason())) {
    holder.remarkOrReasonTextView.setText("null");
}
  1. 日期格式化错误修复
    在ManagePracticeConfirmationActivity的updateButton点击事件中,documentDateFormat.format(practiceDate)会抛出异常(practiceDate是String类型,format方法需要传入Date对象),修改如下:
// 重新获取Date对象
Date practiceDateObj = Calendar.getInstance().getTime();
SimpleDateFormat documentDateFormat = new SimpleDateFormat("dd_MM_yyyy");
String docDate = documentDateFormat.format(practiceDateObj);
  1. 数据刷新逻辑优化
    更新Firestore数据后,仅调用manageAttendanceRecyclerViewAdapter.notifyDataSetChanged()不会更新列表数据(practiceList未同步最新数据),建议在更新成功后重新拉取数据:
collectionReference.document(docAddress)
        .set(confirmation)
        .addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void unused) {
                Toast.makeText(ManagePracticeConfirmationActivity.this, "Attendance marked!", Toast.LENGTH_SHORT)
                        .show();
                // 重新拉取最新数据
                reloadAttendanceData();
            }
        });

// 新增数据刷新方法
private void reloadAttendanceData() {
    practiceList.clear();
    collectionReference.whereEqualTo("practiceDate", practiceDate)
            .get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    if (!queryDocumentSnapshots.isEmpty()) {
                        for (QueryDocumentSnapshot snapshot : queryDocumentSnapshots) {
                            Confirmation confirmation = snapshot.toObject(Confirmation.class);
                            practiceList.add(confirmation);
                        }
                        manageAttendanceRecyclerViewAdapter.notifyDataSetChanged();
                    }
                }
            });
}

内容的提问来源于stack exchange,提问作者Nobil Gautam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 02:35:17