有以下适配器:
private class RepeatingAdapter extends ArrayAdapter<Repeatable> {
private List<Repeatable> items;
private LayoutInflater inflater;
private int resource;
public RepeatingAdapter(Context context, int resource,
List<Repeatable> items) {
super(context, resource, items);
this.items=items;
this.resource=resource;
inflater=LayoutInflater.from(context);
}
@Override
public View getView(int position, View view, ViewGroup group) {
View item=(view==null) ? inflater.inflate(resource, null) : view;
TextView title=(TextView)item.findViewById(R.id.listItemRepeatingTypeTitle);
title.setText(items.get(position).getTitle());
items.get(position).setCommand(new RedRectangleCommand(item));
Log.e("view", item.toString());
return item;
}
@Override
public Repeatable getItem(int position) {
return items.get(position);
}
}
请注意,我们创建了新的 RedRectangleCommand 并将创建的 View 发送给它。所以,我们还做了以下事情:
repeatingList.setAdapter(new RepeatingAdapter(this,
R.layout.list_item_repeating_type, types));
repeatingList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
((Repeatable)parent.getItemAtPosition(position)).mark();
}
});
请注意,我们对选定的项目执行 mark() 方法(可重复的 Mark 方法执行命令的 mark() 方法)。一切都很好。最后的命令代码:
private class RedRectangleCommand extends Command {
private View view;
public RedRectangleCommand(View view) {
this.view=view;
}
@Override
public void mark() {
ImageView image=(ImageView)view.findViewById(R.id.listItemRepeatingTypeImage);
image.setBackgroundColor(Color.RED);
image.invalidate();
}
@Override
public void unmark() {
ImageView image=(ImageView)view.findViewById(R.id.listItemRepeatingTypeImage);
image.setBackgroundColor(Color.BLACK);
}
}
我需要通过单击从选定的视图中更改 ImageView 的颜色。但它不起作用!另外,我的日志显示,从 Command 中选择的项目和项目是不同的。这是怎么回事?