gpt4 book ai didi

android - viewholder 中一个按钮的转换会影响不同行的多个按钮

转载 作者:行者123 更新时间:2023-11-30 01:40:26 25 4
gpt4 key购买 nike

我在我的代码中遇到了这个问题,当在 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 当您点击按钮时。

Then 只是一个 if else 检查的例子

如果状态为 on,则以 0 时间为按钮设置动画,否则取消动画,这样它就不再为按钮设置动画

关于android - viewholder 中一个按钮的转换会影响不同行的多个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34579422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com