有以下适配器:
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中选择的项目和项目是不同的。这是怎么回事?
答案 0 :(得分:0)
也许你可以尝试这个,看看它是否有所不同。它的作用是尝试直接引用RedRectangleCommand对象,使其成为自定义适配器的方法:
RepeatingAdapter rAdapter = new RepeatingAdapter(this,
R.layout.list_item_repeating_type, types);
repeatingList.setAdapter(rAdapter);
repeatingList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
rAdapter.markCommand(position);
}
});
然后在你的适配器中:
private class RepeatingAdapter extends ArrayAdapter {
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;
// .... some code
public void markCommand(int position) {
items.get(position).mark();
}
}
另外我认为真正的问题是你的getView中的那部分:
items.get(position).setCommand(new RedRectangleCommand(item));
因为到目前为止,只要listView行从视图中消失并且已经返回,它正在制作这些对象中的新对象,重新实例化,无论是否被使用都是无关紧要的再次。你可以想象,如果试图引用特定的实例,它可能会多么浪费和麻烦。应该有几种方法来解决这个问题。例如,如果Repeater还没有,则只能执行setCommand。也许做一个布尔方法来检查它是否有一个RedRectangleCommand或类似检查convertView是否为null。我想要做的另一个想法是做一些构造函数工作,但需要一个View
参数似乎很麻烦:
private View item;
public RepeatingAdapter(Context context, int resource,
List<Repeatable> items) {
super(context, resource, items);
this.items=items;
this.resource=resource;
inflater=LayoutInflater.from(context);
item = inflater.inflate(resource, null);
for (int i = 0; i < items.size; i++) {
items.get(i).setCommand(new RedRectangleCommand(item));
}