viewholder 中一个按钮的转换会影响不同行的多个按钮
transition on one button in viewholder affects the multiple buttons of different rows
我的代码中遇到了这个问题,当在 Viewholder 中的一个按钮上进行转换时,这是在 Onclicklistener 中完成的,在不同的行上会发生多个转换
即,如果我单击第 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;
}
}
来自评论
解决该问题的一种方法是添加和排列项目,例如 state
,最初设置为 off
,然后在单击按钮时将其设置为 on
。
那么就是一个if else
检查
的例子
如果状态为打开,则以 0 时间为按钮设置动画,否则取消动画,这样它就不再为按钮设置动画
我的代码中遇到了这个问题,当在 Viewholder 中的一个按钮上进行转换时,这是在 Onclicklistener 中完成的,在不同的行上会发生多个转换 即,如果我单击第 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;
}
}
来自评论
解决该问题的一种方法是添加和排列项目,例如 state
,最初设置为 off
,然后在单击按钮时将其设置为 on
。
那么就是一个if else
检查
如果状态为打开,则以 0 时间为按钮设置动画,否则取消动画,这样它就不再为按钮设置动画