我创建了一个自定义列表视图,在每行的左侧有两个箭头(向上/向下投票)。使用选择器,我已经能够更改箭头的图像(颜色)并相应地更新投票。我的问题是,例如,如果我单击“向上投票”,只要我向下滚动到足够远,以至于我投票的列表项离开屏幕,它就会重置为原始的未选中状态/图像。
这是我的获取视图方法
public View getView(final int position, View convertView, ViewGroup parent)
{
//creating the ViewHolder we defined earlier.
final ViewHolder holder = new ViewHolder();
//creating LayoutInflator for inflating the row layout.
LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflating the row layout we defined earlier.
convertView = inflator.inflate(R.layout.partylist, null);
//setting the views into the ViewHolder.
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.voteUp = (ImageView) convertView.findViewById(R.id.vote_up_image);
holder.voteUp.setTag(position);
holder.voteDown = (ImageView) convertView.findViewById(R.id.vote_down_image);
holder.voteDown.setTag(position);
holder.votes = (TextView) convertView.findViewById(R.id.votes);
//define an onClickListener for the ImageView.
holder.voteUp.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (v.isSelected()){
v.setSelected(false);
holder.votes.setText(String.valueOf((list.get(position).votes)));
} else {
if (holder.voteDown.isSelected()){
v.setSelected(true);
holder.voteDown.setSelected(false);
holder.votes.setText(String.valueOf((list.get(position).votes+1)));
}else{
v.setSelected(true);
}
}
//Toast.makeText(PartyActivity.this, "Image from row " + position + " was pressed " + list.get(position).title, Toast.LENGTH_LONG).show();
}
});
holder.voteDown.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View w)
{
if (w.isSelected()){
w.setSelected(false);
holder.votes.setText(String.valueOf((list.get(position).votes)));
} else {
if (holder.voteUp.isSelected()){
w.setSelected(true);
holder.voteUp.setSelected(false);
holder.votes.setText(String.valueOf((list.get(position).votes-1)));
}else{
w.setSelected(true);
}
}
//Toast.makeText(PartyActivity.this, "Image from row " + position + " was pressed " + list.get(position).title, Toast.LENGTH_LONG).show();
}
});
//setting data into the the ViewHolder.
holder.title.setText(list.get(position).title);
holder.votes.setText(String.valueOf(list.get(position).votes));
//return the row view.
return convertView;
}
}