在我拥有的 Android 应用程序中,有一些ListView
用户可以更改行元素的地方。例如,我有一个按钮,您可以在其中对用户评论进行投票(想想 Facebook“喜欢”或 Google+“+1”)。
当用户单击按钮时,可绘制图像会发生变化。这工作得很好。 问题:
当用户向下滚动然后返回时,状态不会保存,它会返回到原始可绘制对象。
这是我在方法中执行代码的adapter
getView()
方式:
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
dCBlue = (Drawable) getContext().getResources().getDrawable(
R.drawable.ic_comment_blue);
dCGray = (Drawable) getContext().getResources().getDrawable(
R.drawable.ic_comment_gray);
convertView = inflater.inflate(R.layout.review_layout, parent,
holder = new ViewHolder();
holder.b2 = (Button) convertView.findViewById(R.id.bReview2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
convertView.setOnCreateContextMenuListener(null);
}
// later on down
Review r = getItem(position);
votedOn = r.wasVoted;
final Drawable dVBlue = (Drawable) getContext().getResources()
.getDrawable(R.drawable.ic_upvote_blue);
final Drawable dVGray = (Drawable) getContext().getResources()
.getDrawable(R.drawable.ic_upvote_gray);
if (votedOn.equals("1")) {
holder.b2.setCompoundDrawablesWithIntrinsicBounds(null, null, dVBlue, null);
holder.b2.setTag(R.drawable.review_button_press);
} else {
holder.b2.setCompoundDrawablesWithIntrinsicBounds(null, null,dVGray, null);
holder.b2.setTag(R.drawable.review_button);
}
holder.b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (holder.b2.getTag().equals(R.drawable.review_button)) {
holder.b2.setCompoundDrawablesWithIntrinsicBounds(null, null, dVBlue, null);
holder.b2.setTag(R.drawable.review_button_press);
} else {
holder.b2.setCompoundDrawablesWithIntrinsicBounds(null, null, dVGray, null);
holder.b2.setTag(R.drawable.review_button);
}
bVote = holder.b2;
new HelpfulTask().execute(passing);
});
return convertView;
}
我把这个安排得乱七八糟了吗?