我正在开发一个带有ListActivity的Android 2.3.3应用程序。ListView项目:将背景颜色更改为上次选择的项目(或单击)
我想做一些事情,向用户展示他/她选择的项目是什么。我做了以下,但它没有(我在搜索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>
这是对资源的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
。
如何将背景颜色设置为蓝色,以点击最后列表项目?与此列表中使用
UPDATE
阵列适配器:
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;
}
}
你想为当前选择的颜色i项目或最后一个项目点击? – 2012-07-16 14:14:47
我想要选择最后一个项目的颜色。我不知道列表项是否具有选定的状态。 – VansFannel 2012-07-16 14:25:51