你好,我已经在论坛上搜索了几个小时,并决定提出一个问题,因为我真的找不到我的代码有什么问题。我有一个 ListView 后跟一个自定义适配器。我的 listView 中的每个项目看起来像这样 "|txtView| |Btn 0| |Btn 1| |Btn 2|" 我正在使用 ViewHolder 来提高性能。我使用自定义适配器中的 setOnClickListener 。单击的每个按钮都应将其背景更改为绿色,其他按钮更改为灰色。
我的问题是,当单击某个行项目的某个按钮时,它还会更改另一行另一个按钮的背景。我似乎没有找到我的问题,我猜这与我使用 ViewHolder 的重用能力有关。
希望各位大神帮忙,万分感谢。
这是我在适配器中的 getView
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.bet_list_item, null);
holder = new ViewHolder();
holder.tvGameDescription = (TextView) convertView
.findViewById(R.id.gameDescription);
holder.button0 = (Button) convertView
.findViewById(R.id.button0);
holder.button1 = (Button) convertView
.findViewById(R.id.button1);
holder.button2 = (Button) convertView
.findViewById(R.id.button2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
MyOnclickListener myOnclickListener = new MyOnclickListener(holder);
holder.buttonSide1.setOnClickListener(myOnclickListener);
holder.buttonSideX.setOnClickListener(myOnclickListener);
holder.buttonSide2.setOnClickListener(myOnclickListener);
这是监听器实现:
private class MyOnclickListener implements OnClickListener {
private ViewHolder viewHolder;
boolean[] buttonsClickStatus = { false, false, false }; //all gray at start and not clicked
public MyOnclickListener(ViewHolder viewHolder) {
this.viewHolder = viewHolder;
}
@Override
public void onClick(View v) {
switch (((Data) v.getTag()).getBtnPosition()) {
case Consts.BUTTON_0:
if (!buttonsClickStatus[0]) { // case the btn is gray unclicked
setButtonsaBackground(0); // changes the background
buttonsClickStatus[0] = true;
buttonsClickStatus[1] = false;
buttonsClickStatus[2] = false;
} else { // case already green clicked already
addOrRemove = false;
setButtonsaBackground(3);
for (int i = 0; i < buttonsClickStatus.length; i++) {
buttonsClickStatus[i] = false;
}
}
break;
case Consts.BUTTON_1:
if (!buttonsClickStatus[1]) { // case gray
setButtonsaBackground(1);
buttonsClickStatus[1] = true;
buttonsClickStatus[0] = false;
buttonsClickStatus[2] = false;
} else { // case already green
addOrRemove = false;
setButtonsaBackground(3);
for (int i = 0; i < buttonsClickStatus.length; i++) {
buttonsClickStatus[i] = false;
}
}
break;
case Consts.BUTTON_2:
if (!buttonsClickStatus[2]) { // case gray
setButtonsaBackground(2);
buttonsClickStatus[2] = true;
buttonsClickStatus[0] = false;
buttonsClickStatus[1] = false;
} else { // case already green
addOrRemove = false;
setButtonsaBackground(3);
for (int i = 0; i < buttonsClickStatus.length; i++) {
buttonsClickStatus[i] = false;
}
}
break;
default:
break;
}
//call a function to update data only in the activity
myActivity.update((Data) v.getTag());
}
SetBackground 作为私有方法在侦听器内部:
private void setButtonsaBackground(int clicked) {
switch (clicked) {
case 0:
viewHolder.button0.setBackgroundColor(Color.GREEN);
viewHolder.button1.setBackgroundColor(Color.GRAY);
viewHolder.button2.setBackgroundColor(Color.GRAY);
break;
case 1:
viewHolder.button1.setBackgroundColor(Color.GREEN);
viewHolder.button0.setBackgroundColor(Color.GRAY);
viewHolder.button2.setBackgroundColor(Color.GRAY);
break;
case 2:
viewHolder.button2.setBackgroundColor(Color.GREEN);
viewHolder.button0.setBackgroundColor(Color.GRAY);
viewHolder.button1.setBackgroundColor(Color.GRAY);
break;
case 3:
viewHolder.button2.setBackgroundColor(Color.GRAY);
viewHolder.button0.setBackgroundColor(Color.GRAY);
viewHolder.button1.setBackgroundColor(Color.GRAY);
break;
default:
break;
}
}