ListView 选择器问题:选择内容未被删除
我有一个列表视图。当我单击 ListItem 时,我将 ListItem (它的视图)的背景设置为另一种颜色:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
setupDetailView(position);
setupChartView(position);
setupARView(position);
emptyView.setVisibility(View.INVISIBLE);
quotesAdapter.isSelected = true;
//v.setBackgroundResource(R.drawable.stocks_selector);
}
});
这是我的适配器:
private class QuoteAdapter extends ArrayAdapter<Quote> {
private ArrayList<Quote> items;
public boolean isSelected = false;
public QuoteAdapter(Context context, int textViewResourceId, ArrayList<Quote> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.mainrow, null);
if(isSelected)
v.setBackgroundResource(R.drawable.red);
else
v.setBackgroundResource(R.drawable.transparent_background);
}
问题是,如果我选择多行,则多行具有彩色背景。我只希望单击的项目具有彩色背景。因此,如果我单击第 2 行,我希望它变为红色,然后如果我单击第 1 行,我希望第 2 行恢复正常,第 1 行变为红色。
我该怎么做?
I have a ListView. When I click on a ListItem, I set the background of the ListItem (it's view) to another color:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
setupDetailView(position);
setupChartView(position);
setupARView(position);
emptyView.setVisibility(View.INVISIBLE);
quotesAdapter.isSelected = true;
//v.setBackgroundResource(R.drawable.stocks_selector);
}
});
here is my adapter:
private class QuoteAdapter extends ArrayAdapter<Quote> {
private ArrayList<Quote> items;
public boolean isSelected = false;
public QuoteAdapter(Context context, int textViewResourceId, ArrayList<Quote> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.mainrow, null);
if(isSelected)
v.setBackgroundResource(R.drawable.red);
else
v.setBackgroundResource(R.drawable.transparent_background);
}
The problem is, if I select multiple rows, then multiple rows have a colored background. I only want the clicked item to have a colored background. So if I click on row 2, I want it to turn red, then if I click row 1, I want row 2 to go back to normal, and row 1 to turn red.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将列表选择器放入 ListView 中,
stocks_selector.xml 应该类似于
Put your list selector in your ListView
your stocks_selector.xml should look something like