我的代码中遇到了这个问题,当在onclicklistener中在viewholder中的一个按钮上进行转换时,在不同的行上发生了多次转换 即如果我点击第1行中的按钮,则按钮向右移动,这是预期的,但第6行中的按钮也向右移动,这是不期望的。 我已检查第1行中按钮的转换ID与按钮是第6行相同 我不知道,我的代码出了什么问题。请帮忙。 检查我的代码。
public class UserCustomAdapter extends ArrayAdapter<User> {
Context context;
int layoutResourceId;
ArrayList data = new ArrayList();
String weekDay;
AlertDialog alert;
public UserCustomAdapter(Context context, int layoutResourceId,
ArrayList<User> data, String weekDay) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
this.weekDay = weekDay;
}
@Override
public View getView(final int position, final View convertView, final
ViewGroup parent) {
View row = convertView;
final UserHolder holder;
if (row == null) {
LayoutInflater inflater1 = ((Activity) context).getLayoutInflater();
row = inflater1.inflate(layoutResourceId, parent, false);
holder = new UserHolder();
holder.subject = (TextView) row.findViewById(R.id.subject);
holder.percentage = (TextView) row.findViewById(R.id.percentage_edit);
holder.attended = (Button) row.findViewById(R.id.attended);
holder.bunked = (Button) row.findViewById(R.id.bunked);
holder.other = (Button) row.findViewById(R.id.other);
row.setTag(holder);
}
else {
holder = (UserHolder) row.getTag();
}
//to set the text into the fields
User user = (User) data.get(position);
holder.subject.setText(user.getName());
holder.percentage.setText(user.getAddress());
holder.attended.setId(position);
holder.bunked.setId(position);
holder.other.setId(position);
holder.attended.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// FOR ANIMATIONS OF BUTTONS
TranslateAnimation translate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.28f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
translate.setDuration(600);
holder.attended.startAnimation(translate);
Log.d("trans", holder.attended + "");
translate.setFillAfter(true);
TranslateAnimation translate1 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
translate1.setDuration(600);
holder.other.startAnimation(translate1);
translate1.setFillAfter(true);
TranslateAnimation translate2 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
translate2.setDuration(600);
holder.bunked.startAnimation(translate2);
translate2.setFillAfter(true);
}
});
return row;
}
static class UserHolder {
TextView subject;
TextView percentage;
Button attended;
Button bunked;
Button other;
}
}
答案 0 :(得分:1)
来自评论
解决问题的方法是添加和数组项目,例如state
,最初设置为off
,然后在单击按钮时将其设置为on
。
然后只是一个if else
检查
如果状态为0时刻的按钮设置动画或者取消动画,那么它不会为任何更多按钮设置动画