我已經創建了一個回收視圖,它有一個在每一行都有一個複選框的記錄列表。對應於每個複選框,記錄列表中都有一個標誌。我已經爲相同的實現了一個適配器和一個自定義偵聽器。但是點擊價值並沒有在屏幕上發生變化和反映。CheckBox沒有切換點擊RecycleView Android
public class RecycleStudentsAdapter
extends RecyclerView.Adapter<RecycleStudentsAdapter.ViewHolder> {
private final TypedValue mTypedValue = new TypedValue();
private int mBackground;
private List<Student> mValues;
Student template;
public Student getValueAt(int position) {
return mValues.get(position);
}
public RecycleStudentsAdapter(Context context, List<Student> items) {
context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
mBackground = mTypedValue.resourceId;
mValues = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_student, parent, false);
view.setBackgroundResource(mBackground);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
template = null;
template = (Student) mValues.get(position);
holder.studentId.setText(template.getStudent_disp_id());
holder.studentName.setText(template.getStudent_name());
holder.studentCheckBox.setChecked(template.isCheckFlag());
holder.studentCheckBox.setTag(position);
holder.studentCheckBox.setOnClickListener(new CheckBoxListener(position));
holder.studentCheckBox.setClickable(true);
}
public void updateData(List<Student> studentList){
mValues = studentList;
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return mValues.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView studentId;
public final TextView studentName;
public final CheckBox studentCheckBox;
public ViewHolder(View view) {
super(view);
mView = view;
studentId = (TextView) view.findViewById(R.id.studentId);
studentName = (TextView) view.findViewById(R.id.studentName);
studentCheckBox = (CheckBox) view.findViewById(R.id.check);
}
}
class CheckBoxListener implements View.OnClickListener {
int position;
CheckBoxListener(int position) {
this.position = position;
}
public void onClick(View v) {
boolean isChecked = ((CheckBox) v).isChecked();
((CheckBox) v).setChecked(!isChecked);
if (((CheckBox) v).isChecked()) {
studentList.get(position).setCheckFlag(true);
} else {
studentList.get(position).setCheckFlag(false);
}
notifyDataSetChanged();
}
}
}
這是行不通的。請大家分享一些例子,如果可能的 – bond007