请看图片。
这是listview,它的项目是textview,我想结果是当我点击该项目时,其textview的颜色变为红色,而其他所有颜色都没有选择为黑色。
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView one = (TextView) view.findViewById(R.id.item);
one.setTextColor(Color.parseColor("#bb0e0e"));
}
});
我尝试这样,但是当你点击该项目时,所有人点击项目都会变成红色。我怎么能满足自己的需求?
答案 0 :(得分:1)
定义全局int变量
int tempLocation;
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
tempLocation = position
// adapter notify dataset change
adapter.notifyDatasetChanged()
}
});
并覆盖适配器的getView方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = View.inflate(context, R.layout.item_list, null);
if (position == tempLocation) {
// set your color
}
return view;
}
答案 1 :(得分:0)
setcolor refresh listview
之后 iew.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView one = (TextView) view.findViewById(R.id.item);
one.setTextColor(Color.parseColor("#bb0e0e"));
adapter.notifyDataSetChanged();
}
});