如何使用数据绑定在 recyclerview 中获取选定的 textView 值(当前位置)?
How to get selected textView value(current position) in recyclerview using data binding?
我有一个 recyclerview
使用 databinding
。
我需要在 onClick 中从 recyclerview
布局中获取选定的 textview
值,但我不知道如何获取它。
我在 google 上搜索并找到了一些解决方案,但无法解决我的问题。
谁能帮帮我?
适配器:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.itemHolder> {
private StudentAllSessionsAdapter.ClickEvent clickEvent;
private List<StudentEntry> studentList;
private RecyclerViewAdapterBinding binding;
.
.
.
public RecyclerViewAdapter.itemHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.recyclerview_student_all_session, parent, false);
clickEvent = new RecyclerViewAdapter.ClickEvent();
binding.setClickEvent(clickEvent);
return new RecyclerViewAdapter.itemHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull @NotNull RecyclerViewAdapter.itemHolder holder, int position) {
StudentEntry studentEntry = studentList.get(position);
holder.itemView.setStudentEntry(studentEntry);
int scoreSum = database.attendanceDao().getAllScoreStudent(studentEntry.getClassId(), studentEntry.getStudentId());
holder.itemView.textViewItemRecyclerViewStudentListScore.setText(String.valueOf(scoreSum));
}
public class ClickEvent {
public void onClickScore(int classId, String studentId, String studentName) {
//I need to get the TextView value of the current position here
}
}
}
RecyclerView 布局:
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="clickEvent"
type="com.example.user.classmanager.RecyclerViewAdapter.ClickEvent" />
<variable
name="studentEntry"
type="com.example.user.classmanager.database.StudentEntry" />
</data>
<RelativeLayout
android:id="@+id/relativeLayout_item_recyclerView_StudentList"
style="@style/RecyclerVew_BackGround_all"
>
<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)}" />
<TextView
android:id="@+id/textView_item_recyclerView_StudentList_score"
style="@style/TextView_item_recyclerView_StudentList_values" />
</RelativeLayout>
</layout>
我找到了一个简单的方法。
1st : 在 onClick 按钮中添加一个新参数以从 textView(当前位置)获取文本 :
<Button
android:id="@+id/button_item_recyclerView_StudentList_saveScore"
style="@style/Button_item_recyclerView_StudentList_saveAbsent"
android:onClick="@{() -> clickEvent.onClickScore(textViewItemRecyclerViewStudentListScore.getText().toString() , studentEntry.studentId, studentEntry.studentName)}" />
注意: 新参数采用 CamelCase 名称格式。
2nd : 获取onClick中的新参数(textView值) :
public void onClickScore(String textViewValue, int classId, String studentId, String studentName) {
//Do whatever you want with textViewValue
}
我有一个 recyclerview
使用 databinding
。
我需要在 onClick 中从 recyclerview
布局中获取选定的 textview
值,但我不知道如何获取它。
我在 google 上搜索并找到了一些解决方案,但无法解决我的问题。
谁能帮帮我?
适配器:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.itemHolder> {
private StudentAllSessionsAdapter.ClickEvent clickEvent;
private List<StudentEntry> studentList;
private RecyclerViewAdapterBinding binding;
.
.
.
public RecyclerViewAdapter.itemHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.recyclerview_student_all_session, parent, false);
clickEvent = new RecyclerViewAdapter.ClickEvent();
binding.setClickEvent(clickEvent);
return new RecyclerViewAdapter.itemHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull @NotNull RecyclerViewAdapter.itemHolder holder, int position) {
StudentEntry studentEntry = studentList.get(position);
holder.itemView.setStudentEntry(studentEntry);
int scoreSum = database.attendanceDao().getAllScoreStudent(studentEntry.getClassId(), studentEntry.getStudentId());
holder.itemView.textViewItemRecyclerViewStudentListScore.setText(String.valueOf(scoreSum));
}
public class ClickEvent {
public void onClickScore(int classId, String studentId, String studentName) {
//I need to get the TextView value of the current position here
}
}
}
RecyclerView 布局:
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="clickEvent"
type="com.example.user.classmanager.RecyclerViewAdapter.ClickEvent" />
<variable
name="studentEntry"
type="com.example.user.classmanager.database.StudentEntry" />
</data>
<RelativeLayout
android:id="@+id/relativeLayout_item_recyclerView_StudentList"
style="@style/RecyclerVew_BackGround_all"
>
<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)}" />
<TextView
android:id="@+id/textView_item_recyclerView_StudentList_score"
style="@style/TextView_item_recyclerView_StudentList_values" />
</RelativeLayout>
</layout>
我找到了一个简单的方法。
1st : 在 onClick 按钮中添加一个新参数以从 textView(当前位置)获取文本 :
<Button
android:id="@+id/button_item_recyclerView_StudentList_saveScore"
style="@style/Button_item_recyclerView_StudentList_saveAbsent"
android:onClick="@{() -> clickEvent.onClickScore(textViewItemRecyclerViewStudentListScore.getText().toString() , studentEntry.studentId, studentEntry.studentName)}" />
注意: 新参数采用 CamelCase 名称格式。
2nd : 获取onClick中的新参数(textView值) :
public void onClickScore(String textViewValue, int classId, String studentId, String studentName) {
//Do whatever you want with textViewValue
}