Radiogroup 在 recyclerview 中交换位置
Radiogroup swaps position in recyclerview
当我点击复选框时,收音机将可见,当我取消选中它时,它不会 visible.but 当我向下滚动并再次查看时,如果我 uncheck.and 收音机组将是可见的当我向下滚动时它会重复。
private List<MarkAttendance_info> stList;
public MarkAttendance_Adapter(List<MarkAttendance_info> students) {
this.stList = students;
}
// Create new views
@Override
public MarkAttendance_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.mark_attendance, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
final int pos = position;
viewHolder.tvName.setText(stList.get(position).getStudentname());
viewHolder.tvEmailId.setText(stList.get(position).getStudentid());
viewHolder.stupkey.setText(stList.get(position).getStudentPkey());
viewHolder.chkSelected.setChecked(stList.get(position).isSelected());
viewHolder.chkSelected.setTag(stList.get(position));
viewHolder.chkSelected.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
CheckBox cb = (CheckBox) v;
MarkAttendance_info contact = (MarkAttendance_info) cb.getTag();
contact.setSelected(cb.isChecked());
stList.get(pos).setSelected(cb.isChecked());
if(cb.isChecked())
{
viewHolder.grades.setVisibility(View.VISIBLE);
}
else
{
viewHolder.grades.setVisibility(View.GONE);
}
Toast.makeText(
v.getContext(),
"Clicked on Checkbox: " + cb.getText() + " is "
+ cb.isChecked(), Toast.LENGTH_LONG).show();
}
});
viewHolder.grades.setTag(stList.get(position));
setRadio(viewHolder, stList.get(position).getCheckedId());
viewHolder.a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stList.get(position).setCheckedId(0);
setRadio(viewHolder, stList.get(position).getCheckedId());
}
});
viewHolder.b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
stList.get(position).setCheckedId(1);
setRadio(viewHolder, stList.get(position).getCheckedId());
}
});
viewHolder.c.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stList.get(position).setCheckedId(2);
setRadio(viewHolder, stList.get(position).getCheckedId());
}
});
viewHolder.d.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stList.get(position).setCheckedId(3);
setRadio(viewHolder, stList.get(position).getCheckedId());
}
});
viewHolder.e.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stList.get(position).setCheckedId(4);
setRadio(viewHolder, stList.get(position).getCheckedId());
}
});
}
private void setRadio(final ViewHolder holder, int selection) {
System.out.println("SELECT:" + selection);
RadioButton b1 = holder.a;
RadioButton b2 = holder.b;
RadioButton b3 = holder.c;
RadioButton b4 = holder.d;
RadioButton b5 = holder.e;
if (selection == 0) b1.setChecked(true);
if (selection == 1) b2.setChecked(true);
if (selection == 2) b3.setChecked(true);
if (selection == 3) b4.setChecked(true);
if (selection == 4) b5.setChecked(true);
}
// Return the size arraylist
@Override
public int getItemCount()
{
return stList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvName;
public TextView tvEmailId;
public TextView stupkey;
public CheckBox chkSelected;
public RadioGroup grades;
public RadioButton a;
public RadioButton b;
public RadioButton c;
public RadioButton d;
public RadioButton e;
public MarkAttendance_info singlestudent;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
tvName = (TextView) itemLayoutView.findViewById(R.id.txt_student_name);
tvEmailId = (TextView) itemLayoutView.findViewById(R.id.txt_student_id);
stupkey =(TextView) itemLayoutView.findViewById(R.id.student_primkey);
grades = (RadioGroup) itemLayoutView.findViewById(R.id.radioGroup_grade);
a =(RadioButton) itemLayoutView.findViewById(R.id.radioButton_A);
b =(RadioButton) itemLayoutView.findViewById(R.id.radioButton_B);
c =(RadioButton) itemLayoutView.findViewById(R.id.radioButton_C);
d =(RadioButton) itemLayoutView.findViewById(R.id.radioButton_D);
e =(RadioButton) itemLayoutView.findViewById(R.id.radioButton_E);
chkSelected = (CheckBox) itemLayoutView
.findViewById(R.id.radioButtonPresent);
}
}
// method to access in activity after updating selection
public List<MarkAttendance_info> getStudentLists() {
return stList;
}
}
将此行添加到 onBindViewHolder
:
if(stList.get(position).isSelected()) {
//hide the layout you need - i.e. set the WHOLE layout View.GONE
}
只需放入您的 getView()
viewHolder.grades.setVisibility(cb.isChecked() ? View.VISIBLE : View.GONE);
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
final int pos = position;
viewHolder.tvName.setText(stList.get(position).getStudentname());
viewHolder.tvEmailId.setText(stList.get(position).getStudentid());
viewHolder.stupkey.setText(stList.get(position).getStudentPkey());
viewHolder.chkSelected.setChecked(stList.get(position).isSelected());
viewHolder.chkSelected.setTag(stList.get(position));
viewHolder.chkSelected.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
CheckBox cb = (CheckBox) v;
MarkAttendance_info contact = (MarkAttendance_info) cb.getTag();
contact.setSelected(cb.isChecked());
stList.get(pos).setSelected(cb.isChecked());
if(cb.isChecked())
{
viewHolder.grades.setVisibility(View.VISIBLE);
}
else
{
viewHolder.grades.setVisibility(View.GONE);
}
Toast.makeText(
v.getContext(),
"Clicked on Checkbox: " + cb.getText() + " is "
+ cb.isChecked(), Toast.LENGTH_LONG).show();
}
});
viewHolder.grades.setTag(stList.get(position));
if (chkSelected.isChecked()) {
viewHolder.grades.setVisibility(View.VISIBLE);
} else {
viewHolder.grades.setVisibility(View.GONE);
}
// ...
}
当我点击复选框时,收音机将可见,当我取消选中它时,它不会 visible.but 当我向下滚动并再次查看时,如果我 uncheck.and 收音机组将是可见的当我向下滚动时它会重复。
private List<MarkAttendance_info> stList;
public MarkAttendance_Adapter(List<MarkAttendance_info> students) {
this.stList = students;
}
// Create new views
@Override
public MarkAttendance_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.mark_attendance, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
final int pos = position;
viewHolder.tvName.setText(stList.get(position).getStudentname());
viewHolder.tvEmailId.setText(stList.get(position).getStudentid());
viewHolder.stupkey.setText(stList.get(position).getStudentPkey());
viewHolder.chkSelected.setChecked(stList.get(position).isSelected());
viewHolder.chkSelected.setTag(stList.get(position));
viewHolder.chkSelected.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
CheckBox cb = (CheckBox) v;
MarkAttendance_info contact = (MarkAttendance_info) cb.getTag();
contact.setSelected(cb.isChecked());
stList.get(pos).setSelected(cb.isChecked());
if(cb.isChecked())
{
viewHolder.grades.setVisibility(View.VISIBLE);
}
else
{
viewHolder.grades.setVisibility(View.GONE);
}
Toast.makeText(
v.getContext(),
"Clicked on Checkbox: " + cb.getText() + " is "
+ cb.isChecked(), Toast.LENGTH_LONG).show();
}
});
viewHolder.grades.setTag(stList.get(position));
setRadio(viewHolder, stList.get(position).getCheckedId());
viewHolder.a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stList.get(position).setCheckedId(0);
setRadio(viewHolder, stList.get(position).getCheckedId());
}
});
viewHolder.b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
stList.get(position).setCheckedId(1);
setRadio(viewHolder, stList.get(position).getCheckedId());
}
});
viewHolder.c.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stList.get(position).setCheckedId(2);
setRadio(viewHolder, stList.get(position).getCheckedId());
}
});
viewHolder.d.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stList.get(position).setCheckedId(3);
setRadio(viewHolder, stList.get(position).getCheckedId());
}
});
viewHolder.e.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stList.get(position).setCheckedId(4);
setRadio(viewHolder, stList.get(position).getCheckedId());
}
});
}
private void setRadio(final ViewHolder holder, int selection) {
System.out.println("SELECT:" + selection);
RadioButton b1 = holder.a;
RadioButton b2 = holder.b;
RadioButton b3 = holder.c;
RadioButton b4 = holder.d;
RadioButton b5 = holder.e;
if (selection == 0) b1.setChecked(true);
if (selection == 1) b2.setChecked(true);
if (selection == 2) b3.setChecked(true);
if (selection == 3) b4.setChecked(true);
if (selection == 4) b5.setChecked(true);
}
// Return the size arraylist
@Override
public int getItemCount()
{
return stList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvName;
public TextView tvEmailId;
public TextView stupkey;
public CheckBox chkSelected;
public RadioGroup grades;
public RadioButton a;
public RadioButton b;
public RadioButton c;
public RadioButton d;
public RadioButton e;
public MarkAttendance_info singlestudent;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
tvName = (TextView) itemLayoutView.findViewById(R.id.txt_student_name);
tvEmailId = (TextView) itemLayoutView.findViewById(R.id.txt_student_id);
stupkey =(TextView) itemLayoutView.findViewById(R.id.student_primkey);
grades = (RadioGroup) itemLayoutView.findViewById(R.id.radioGroup_grade);
a =(RadioButton) itemLayoutView.findViewById(R.id.radioButton_A);
b =(RadioButton) itemLayoutView.findViewById(R.id.radioButton_B);
c =(RadioButton) itemLayoutView.findViewById(R.id.radioButton_C);
d =(RadioButton) itemLayoutView.findViewById(R.id.radioButton_D);
e =(RadioButton) itemLayoutView.findViewById(R.id.radioButton_E);
chkSelected = (CheckBox) itemLayoutView
.findViewById(R.id.radioButtonPresent);
}
}
// method to access in activity after updating selection
public List<MarkAttendance_info> getStudentLists() {
return stList;
}
}
将此行添加到 onBindViewHolder
:
if(stList.get(position).isSelected()) {
//hide the layout you need - i.e. set the WHOLE layout View.GONE
}
只需放入您的 getView() viewHolder.grades.setVisibility(cb.isChecked() ? View.VISIBLE : View.GONE);
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
final int pos = position;
viewHolder.tvName.setText(stList.get(position).getStudentname());
viewHolder.tvEmailId.setText(stList.get(position).getStudentid());
viewHolder.stupkey.setText(stList.get(position).getStudentPkey());
viewHolder.chkSelected.setChecked(stList.get(position).isSelected());
viewHolder.chkSelected.setTag(stList.get(position));
viewHolder.chkSelected.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
CheckBox cb = (CheckBox) v;
MarkAttendance_info contact = (MarkAttendance_info) cb.getTag();
contact.setSelected(cb.isChecked());
stList.get(pos).setSelected(cb.isChecked());
if(cb.isChecked())
{
viewHolder.grades.setVisibility(View.VISIBLE);
}
else
{
viewHolder.grades.setVisibility(View.GONE);
}
Toast.makeText(
v.getContext(),
"Clicked on Checkbox: " + cb.getText() + " is "
+ cb.isChecked(), Toast.LENGTH_LONG).show();
}
});
viewHolder.grades.setTag(stList.get(position));
if (chkSelected.isChecked()) {
viewHolder.grades.setVisibility(View.VISIBLE);
} else {
viewHolder.grades.setVisibility(View.GONE);
}
// ...
}