我正在使用CustomAdapter创建一个ListView。每个ListView的行都有一个切换按钮,我在此library中使用了该按钮。所以这是我的适配器代码
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Attendance attendance = getItem(position);
if(convertView==null){
convertView = LayoutInflater.from(context).inflate(R.layout.student_attendance_list_model,parent,false);
holder = new ViewHolder();
holder.studentName = (TextView)convertView.findViewById(R.id.student_name);
holder.studentSchool = (TextView)convertView.findViewById(R.id.student_school);
holder.bulletIcon = (ImageView) convertView.findViewById(R.id.bullet_img);
holder.toggleButton = (TriStateToggleButton) convertView.findViewById(R.id.tri_state_toggle);
holder.yesText = (TextView)convertView.findViewById(R.id.yes_btn);
holder.noText = (TextView)convertView.findViewById(R.id.no_btn);
holder.toggleButton.setOnToggleChanged(new TriStateToggleButton.OnToggleChanged() {
@Override
public void onToggle(TriStateToggleButton.ToggleStatus toggleStatus, boolean b) {
switch (toggleStatus){
case off:
holder.yesText.setTextColor(context.getResources().getColor(R.color.green));
holder.noText.setTextColor(context.getResources().getColor(R.color.black_text));
break;
case mid:
holder.noText.setTextColor(context.getResources().getColor(R.color.black_text));
holder.yesText.setTextColor(context.getResources().getColor(R.color.black_text));
break;
case on:
holder.noText.setTextColor(context.getResources().getColor(R.color.trial_color));
holder.yesText.setTextColor(context.getResources().getColor(R.color.black_text));
break;
}
}
});
convertView.setTag(holder);
}
else{
holder = (ViewHolder)convertView.getTag();
}
holder.toggleButton.setOnToggleChanged(new TriStateToggleButton.OnToggleChanged() {
@Override
public void onToggle(TriStateToggleButton.ToggleStatus toggleStatus, boolean b) {
LogHelper.debug(getClass(),"pos = "+position);
switch (toggleStatus){
case off:
holder.yesText.setTextColor(context.getResources().getColor(R.color.green));
holder.noText.setTextColor(context.getResources().getColor(R.color.black_text));
break;
case mid:
holder.noText.setTextColor(context.getResources().getColor(R.color.black_text));
holder.yesText.setTextColor(context.getResources().getColor(R.color.black_text));
break;
case on:
holder.noText.setTextColor(context.getResources().getColor(R.color.trial_color));
holder.yesText.setTextColor(context.getResources().getColor(R.color.black_text));
break;
}
}
});
holder.studentName.setText(attendance.getUserInformation().getFullName());
holder.studentSchool.setText(attendance.getUserInformation().getSchool());
try {
switch (attendance.getUserInformation().getStatus()) {
case ("enroll"):
holder.bulletIcon.setImageResource(R.drawable.enrollment_circle);
break;
case ("transfer"):
holder.bulletIcon.setImageResource(R.drawable.transfer_circle);
break;
case ("trial"):
holder.bulletIcon.setImageResource(R.drawable.trial_circle);
break;
case ("replace"):
holder.bulletIcon.setImageResource(R.drawable.replacement_circle);
break;
}
}catch (Exception e){e.printStackTrace();}
switch (attendance.getAttend()){
case ("true"):
holder.yesText.setTextColor(context.getResources().getColor(R.color.green));
holder.noText.setTextColor(context.getResources().getColor(R.color.black_text));
holder.toggleButton.setToggleOff();
break;
case ("false"):
holder.noText.setTextColor(context.getResources().getColor(R.color.trial_color));
holder.yesText.setTextColor(context.getResources().getColor(R.color.black_text));
holder.toggleButton.setToggleOn();
break;
default:
//holder.toggleButton.setMidColor(context.getResources().getColor(R.color.grey_87));
holder.toggleButton.setToggleMid();
holder.noText.setTextColor(context.getResources().getColor(R.color.black_text));
holder.yesText.setTextColor(context.getResources().getColor(R.color.black_text));
break;
}
return convertView;
}
class ViewHolder {
TextView studentName;
TextView studentSchool;
ImageView bulletIcon;
TriStateToggleButton toggleButton;
TextView yesText;
TextView noText;
}
当我更改切换状态时,它返回正确的行的位置,但其yesText和noText不会改变颜色。相反,每次单击任何切换时,触发最后一项中的yesText和noText都会更改其颜色。
Button onClick get wrong position in viewholder patterned listview
上面的链接与我的问题完全相同,但在我的情况下,我不了解notifyDataSetChanged()的实现。
由于
答案 0 :(得分:0)
删除代码以设置switch
中toggleChangeListener
块的文本颜色,而是使用&#34更新该位置的对象; true"," false"和默认值,然后调用notifyDataSetChanged