我有一个Custom ArrayAdapter,用于包含多个按钮的列表视图。但是,当我单击一个按钮时,它的行动与错误的行。例;当我点击第一行的按钮时,它的点击动作正在另一行。
这里有public View getView(final int position, View view, ViewGroup parent) {
this.position = position * 2;
LayoutInflater inflater = context.getActivity().getLayoutInflater();
if (view == null) {
view = inflater.inflate(R.layout.item_galery2_list, parent, false);
holder = new Holder(view);
view.setTag(holder);
} else {
holder = (Holder) view.getTag();
}
((TextView) view.findViewWithTag("textView1")).setText(list
.get(this.position).yazi.toString());
((TextView) view.findViewWithTag("textView2")).setText(list
.get(this.position + 1).yazi.toString());
holder.getImage1().setImageBitmap((list.get(this.position).image));
holder.getImage2().setImageBitmap((list.get(this.position + 1).image));
final Holder h1 = holder;
holder.btn_follow1().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (h1.btn_follow1().getBackground().getConstantState()==
context.getResources().getDrawable(R.drawable.followoff).getConstantState()) {
h1.btn_follow1().setBackgroundResource(R.drawable.followon);
} else if(h1.btn_follow1().getBackground().getConstantState()==
context.getResources().getDrawable(R.drawable.followon).getConstantState()) {
h1.btn_follow1().setBackgroundResource(R.drawable.followoff);
notifyDataSetChanged();
}
}
});
此类持有人代码:
public class Holder {
private View row;
private ImageView img_img;
private ImageView img_img2;
private Button btn_follow1;
private Button btn_follow2;
public Holder(View row) {
this.row = row;
}
public ImageView getImage1() {
if (img_img == null) {
img_img = (ImageView) row.findViewById(R.id.img_img1);
}
return img_img;
}
public ImageView getImage2() {
if (img_img2 == null) {
img_img2 = (ImageView) row.findViewById(R.id.img_img2);
}
return img_img2;
}
public Button btn_follow1() {
if (btn_follow1 == null) {
btn_follow1 = (Button) row.findViewById(R.id.btn_follow1);
}
return btn_follow1;
}
public Button btn_follow2() {
if (btn_follow2 == null) {
btn_follow2 = (Button) row.findViewById(R.id.btn_follow2);
}
return btn_follow2;
}
}
解决此问题的最佳方法是什么?
编辑:
当我点击第一行的关注按钮时;
点击之前:
点击后
答案 0 :(得分:0)
因为this.position = position * 2;
。因此:
您可能想重新考虑如何管理position
变量。在正常情况下(例如:如果用户点击位置0,位置0将被激活),您不需要对position
执行任何操作。