You need to enable JavaScript to run this app. 如何使用Data Binding获取RecyclerView当前位置选中的TextView值
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Data Binding获取RecyclerView当前位置选中的TextView值

解决方案

首先修正现有代码的错误:你当前把RecyclerViewAdapterBinding定义为Adapter全局成员变量的写法是错误的,每个ViewHolder需要持有自己专属的Binding实例,否则会出现条目内容错乱的问题,请先将itemHolder修改为以下结构:

public class itemHolder extends RecyclerView.ViewHolder {
    // 每个ViewHolder持有自己的Binding
    public RecyclerViewAdapterBinding binding;
    public itemHolder(RecyclerViewAdapterBinding binding) {
        super(binding.getRoot());
        this.binding = binding;
    }
}

同时删除Adapter层的全局binding变量,onCreateViewHolder修改为:

public RecyclerViewAdapter.itemHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
    RecyclerViewAdapterBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.recyclerview_student_all_session, parent, false);
    ClickEvent clickEvent = new ClickEvent();
    binding.setClickEvent(clickEvent);
    return new RecyclerViewAdapter.itemHolder(binding);
}

以下是两种获取TextView值的可行方案:

方案一:直接传值到回调(最推荐,符合Data Binding设计规范)

不需要额外查找View,直接把需要的分数作为参数传入点击回调即可

  1. 给布局新增score变量,同时把TextView的内容和score绑定
<data>
    <variable
        name="clickEvent"
        type="com.example.user.classmanager.RecyclerViewAdapter.ClickEvent" />
    <variable
        name="studentEntry"
        type="com.example.user.classmanager.database.StudentEntry" />
    <!-- 新增分数变量 -->
    <variable
        name="score"
        type="java.lang.Integer" />
</data>

<!-- TextView直接绑定score -->
<TextView
    android:id="@+id/textView_item_recyclerView_StudentList_score"
    style="@style/TextView_item_recyclerView_StudentList_values"
    android:text="@{String.valueOf(score)}" />

<!-- 点击事件新增score参数 -->
<Button
    android:id="@+id/button_item_recyclerView_StudentList_saveScore"
    style="@style/Button_item_recyclerView_StudentList_saveAbsent"
    android:onClick="@{() -> clickEvent.onClickScore(studentEntry.classId, studentEntry.studentId, studentEntry.studentName, score)}" />
  1. 修改ClickEvent的回调方法,新增score参数
public class ClickEvent {
    public void onClickScore(int classId, String studentId, String studentName, int score) {
        // 这里直接拿到分数值,无需额外查找View
    }
}
  1. 修改onBindViewHolder逻辑,把计算好的分数设置到Binding中
@Override
public void onBindViewHolder(@NonNull @NotNull RecyclerViewAdapter.itemHolder holder, int position) {
    StudentEntry studentEntry = studentList.get(position);
    holder.binding.setStudentEntry(studentEntry);
    int scoreSum = database.attendanceDao().getAllScoreStudent(studentEntry.getClassId(), studentEntry.getStudentId());
    // 把分数设置到Binding变量
    holder.binding.setScore(scoreSum);
}

方案二:通过点击的View获取Binding拿值

如果不想修改现有回调的参数结构,可以通过点击事件的View实例获取对应条目的Binding,再拿TextView的值

  1. 修改ClickEvent的回调方法,新增View参数
public class ClickEvent {
    public void onClickScore(View view, int classId, String studentId, String studentName) {
        // 从点击的View获取对应条目的Binding
        RecyclerViewAdapterBinding binding = DataBindingUtil.findBinding(view);
        String scoreText = binding.textViewItemRecyclerViewStudentListScore.getText().toString();
        int score = Integer.parseInt(scoreText);
        // 即可拿到分数值
    }
}
  1. 修改布局点击事件,传入当前点击的View
<Button
    android:id="@+id/button_item_recyclerView_StudentList_saveScore"
    style="@style/Button_item_recyclerView_StudentList_saveAbsent"
    android:onClick="@{(view) -> clickEvent.onClickScore(view, studentEntry.classId, studentEntry.studentId, studentEntry.studentName)}" />

内容的提问来源于stack exchange,提问作者Nima Khalili

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.11 16:15:03