【发布时间】:2011-08-11 19:09:05
【问题描述】:
我有一个由 SimpleCursorAdapter 和自定义 ViewBinder 支持的 ListView。我想让这个列表视图中的项目在点击时改变它们的颜色。如果我在 OnClickListener 中执行此操作 - 它会部分工作,每 7 次更改单击的项目的颜色以及列表中项目的颜色(我猜,时间段取决于列表视图的可视区域)。
谁能建议如何处理这个问题?或者,也许指向一种更优雅的方式使 listView 中的项目可选?
谢谢。
UPD:(抱歉格式错误 - 这是我第一次发布问题):
以下是我如何尝试使 ListView 中的项目“选中”:
private void setupListView(final ListView lv) {
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view, int position, final long id) {
RelativeLayout layout = (RelativeLayout) view;
int color;
if (conditionMet) {
color = R.color.gray;
} else {
color = R.color.red;
}
for(int i = 0; i < layout.getChildCount(); i++) {
((TextView)layout.getChildAt(i)).setTextColor(getResources().getColor(color));
}
return;
}}
这就是我初始化适配器的方式:
final SimpleCursorAdapter adapter =
new SimpleCursorAdapter(
this,
itemId,
cursor,
from,
to
);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
final TextView textView = (TextView) view;
// do necessary conversions
return true;
}
});
listView.setAdapter(adapter);
【问题讨论】:
-
添加了一些代码,还有什么要添加的吗?实际上,正如我所说,这部分起作用,因为它确实“突出”了项目本身,但我想它的视图被重用了
标签: android listview text colors onclick