gpt4 book ai didi

android - ListView选择器问题: Selection does not get removed

转载 作者:行者123 更新时间:2023-11-29 22:31:28 24 4
gpt4 key购买 nike

我有一个 ListView。当我点击一个 ListItem 时,我将 ListItem(它的 View )的背景设​​置为另一种颜色:

  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 行变成红色。

我该怎么做?

最佳答案

将列表选择器放在 ListView 中

 <ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:listSelector="@drawable/stocks_selector" />

您的 stocks_selector.xml 应该类似于

<shape
xmlns:android="http://schemas.android.com/apk/res/android"
type="rectangle"

>
<gradient
android:startColor="@color/start_gradient"
android:endColor="@color/end_gradient"
android:angle="270" />
/>
</shape>

关于android - ListView选择器问题: Selection does not get removed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3808175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com