当listview的item被点击时,如何让它的视图颜色变成红色,其他的是黑色?
when listview's item is click,how to make its view color to be red, others is black?
请看图
这是listview,它的item是一个textview,我想要的结果是当我点击item时,它的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"));
}
});
我试过了,但是当你点击这个项目时,所有点击的项目都会变成红色。我该怎么做才能满足我的需求?
setcolor刷新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();
}
});
定义一个全局整型变量
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;
}
请看图
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"));
}
});
我试过了,但是当你点击这个项目时,所有点击的项目都会变成红色。我该怎么做才能满足我的需求?
setcolor刷新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();
}
});
定义一个全局整型变量
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;
}