我正在开发一个带有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>
但它不起作用,因为它说我必须在@drawable
中添加selector.xml
。
如何将背景颜色设置为蓝色到上次列出的列表项?
更新
此列表使用的数组适配器:
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;
}
}
答案 0 :(得分:1)
我已经使用自定义数组适配器完成了它,只需在getView()
方法中检查:
if (position == lastClicked) {
v.setBackgroundColor(0x...)
} else {
v.setBackgroundColor(0x...)
}
由于大型列表视图的特定行为,else块很重要
更新:这是我的自定义数组适配器工作正常:
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;
}
}
答案 1 :(得分:0)
是的,选择器中的项目只接受drawable。但是,您可以使用颜色创建drawable: 在colors.xml中添加颜色:
<drawable name="color1">#00FF00</drawable>
<drawable name="color2">#00FF00</drawable>
<drawable name="color3">#000000</drawable>
然后,将它们用作drawables:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@drawable/color1" />
<item android:state_pressed="true"
android:drawable="@drawable/color2" />
<item android:drawable="@drawable/color3" />
</selector>
希望这会帮助你=)
答案 2 :(得分:0)
我更喜欢这个更简单的解决方案:
content://downloads/all_downloads/...