gpt4 book ai didi

android - ListView 项目 : change background color to last item selected (or clicked)

转载 作者:行者123 更新时间:2023-11-30 04:06:44 25 4
gpt4 key购买 nike

我正在开发一个带有 ListActivity 的 Android 2.3.3 应用程序。

我想做一些事情来向用户展示他/她选择了什么项目。我已经完成了以下操作,但没有(我在 stackoverflow 中搜索,它们是相互矛盾的答案)。

这是layout_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/txtName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector"
android:text="" />

</LinearLayout>

这是res/drawable上的selector.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:color="#00FF00" />
<item android:state_pressed="true"
android:color="#555555" />
<item android:color="#000000" />
</selector>

但它不起作用,因为它说我必须在 selector.xml 中放置一个 @drawable

如何将背景颜色设置为蓝色以显示最后单击的列表项?

更新

与此列表一起使用的数组适配器:

public class GatesAdapter extends ArrayAdapter<Gate>
{
/**
* Application context.
*/
private Context context;
/**
*
*/
private int itemLayoutId;
/**
*
*/
private ArrayList<Gate> gates;
private int selectedGateIndex;

public int getSelectedGateIndex() {
return selectedGateIndex;
}

public void setSelectedGateIndex(int selectedGateIndex) {
this.selectedGateIndex = selectedGateIndex;
}

public Gate getSelectedGate()
{
return gates.get(selectedGateIndex);
}

public void removeSelectedGate()
{
this.gates.remove(selectedGateIndex);
}

public ArrayList<Gate> getGates()
{
return this.gates;
}

public GatesAdapter(Context context, int listItemResourceId,
ArrayList<Gate> objects)
{
super(context, listItemResourceId, objects);

this.context = context;
this.itemLayoutId = listItemResourceId;
this.gates = objects;
this.selectedGateIndex = -1;

this.setNotifyOnChange(true);
}

@Override
public int getCount()
{
return gates.size();
}

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
Log.v("GatesAdapter", "getView.postion: " + position);
View row = convertView;
if (row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(itemLayoutId, parent, false);
}

Gate gate = gates.get(position);
if (gate != null)
{
TextView itemText = (TextView)row.findViewById(android.R.id.text1);
if (itemText != null)
{
itemText.setText(gate.getName());
//selectedGateIndex = position;
if (selectedGateIndex == position)
{
row.setBackgroundColor(Color.BLUE);
}
}
}

return row;
}
}

最佳答案

我已经用自定义数组适配器完成了,只是在 getView() 方法中检查过:

if (position == lastClicked) {
v.setBackgroundColor(0x...)
} else {
v.setBackgroundColor(0x...)
}

由于大型 ListView 的特定行为,else block 很重要

更新:这是我的自定义数组适配器,工作正常:

public class AbcArrayAdapter extends ArrayAdapter<UniversalListItem> {

private Context c;
private int id;
private List<UniversalListItem>items;

public AbcArrayAdapter(Context context, int viewResourceId, List<UniversalListItem> objects){
super(context,viewResourceId,objects);
c=context;
id=viewResourceId;
items=objects;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(id, null);
}

final UniversalListItem o = items.get(position);
if (o != null) {
if ( o.isInList() ) {
v.setBackgroundColor(0x00ffffff);
} else {
v.setBackgroundColor(0x4d0099cc);
}
/*....*/
}
return v;
}

关于android - ListView 项目 : change background color to last item selected (or clicked),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11506116/

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