我一直在尝试使用Custom SimpleAdapter来实现让ListView项目背景颜色根据其状态进行更改。我正在关注此处和其他网站上的代码参考。我有部分工作,但不完全。
首先,我的代码:
包含ListView的总体GUI的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/widget37"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/textViewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Building List..."
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ff000000"
android:textSize="22sp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textViewMessage"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp">
<ListView
android:id="@id/listLocations"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice"
android:clickable="true"
android:scrollingCache="false"
android:background="@color/transparent"
android:listSelector="@drawable/listitem_selector">
</ListView>
</RelativeLayout>
<Button
android:id="@+id/buttonTryAgain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Try Again" />
根据其他帖子,请注意我使用 listitem_selector 作为ListView的背景,以便各个项目可以用不同颜色反映状态。
接下来是listitem_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:drawable="@color/itemSelected" />
<item android:state_pressed="true" android:drawable="@color/itemPressed" />
<item android:drawable="@color/text_white" />
这是一个&#39;皱纹&#39;由于每个项目由4个文本框组成 - 适配器构建为使用布局 list_parkers_tmp
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="@drawable/listitem_selector">
<ImageView
android:id="@+id/logo"
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/hangtag" />
<TextView
android:id="@+id/textViewPermit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/logo"
android:text="Permit Number"
android:textStyle="bold"
android:textColor="#FF00AA00"
android:textSize="22dp" />
<TextView
android:id="@+id/textViewCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textViewPermit"
android:paddingLeft="10dp"
android:text="Category"
android:textColor="#FF555555"
android:textSize="16dp" />
<TextView
android:id="@+id/textViewCars"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/logo"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/logo"
android:text="TextView"
android:textColor="#FF000000"
android:textSize="18dp" />
<TextView
android:id="@+id/textStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingLeft="10dp"
android:text="Status"
android:textStyle="bold"
android:textColor="#FF0000"
android:textSize="16dp" />
活动中的代码:
// create our SelectedAdapter
selectedAdapter = new SelectedAdapter(getBaseContext(), itemList, R.layout.list_parkers_tmp,
new String[]{"permit", "status", "category", "cars"},
new int[]{R.id.textViewPermit, R.id.textStatus, R.id.textViewCategory, R.id.textViewCars});
selectedAdapter.notifyDataSetChanged();
listLocations.setAdapter(selectedAdapter);
最后是Custom SimpleAdapter:
public class SelectedAdapter extends SimpleAdapter {
private int[] mTo;
private String[] mFrom;
private ViewBinder mViewBinder;
private List<? extends Map<String, ?>> mData;
private int mResource;
private int mDropDownResource;
private LayoutInflater mInflater;
public SelectedAdapter(Context context,
List<? extends Map<String, String>> data, int resource, String[] from,
int[] to) {
super(context, data, resource, from, to);
mData = data;
mResource = mDropDownResource = resource;
mFrom = from;
mTo = to;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/**
* @see android.widget.Adapter#getView(int, View, ViewGroup)
*/
// used to keep selected position in ListView
private int selectedPos = -1; // init value for not-selected
public void setSelectedPosition(int pos) {
selectedPos = pos;
// inform the view of this change
notifyDataSetChanged();
}
public int getSelectedPosition() {
return selectedPos;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//View v = convertView;
View v = super.getView(position, convertView, parent);
// only inflate the view if it's null
if (v == null) {
LayoutInflater vi = (LayoutInflater) Config.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//LayoutInflater vi
// = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_parkers_tmp, null);
}
// get text view
String TextViewStr = "";
String CategoryText = "";
TextViewStr = this.getItem(position).toString(); // Get ALL of ListView Item's Text Values
TextView viewPermit = (TextView) v.findViewById(R.id.textViewPermit);
CategoryText = parseItemStr(TextViewStr,"permit=","");
viewPermit.setText(CategoryText);
TextView status = (TextView) v.findViewById(R.id.textStatus);
CategoryText = parseItemStr(TextViewStr,"status=","cars=");
status.setText(CategoryText);
TextView viewCategory = (TextView) v.findViewById(R.id.textViewCategory);
CategoryText = parseItemStr(TextViewStr,"category=","permit=");
viewCategory.setText(CategoryText);
TextView viewCars = (TextView) v.findViewById(R.id.textViewCars);
CategoryText = parseItemStr(TextViewStr,"cars=","category=");
viewCars.setText(CategoryText);
// change the row color based on selected state
if (selectedPos == position) {
v.setSelected(true);
v.setPressed(true);
v.setBackgroundColor(R.color.amber_800);
} else {
v.setSelected(false);
v.setPressed(false);
v.setBackgroundColor(R.color.text_white);
}
/*
// to use something other than .toString()
MyClass myobj = (MyClass)this.getItem(position);
label.setText(myobj.myReturnsString());
*/
return (v);
}
public String parseItemStr (String ItemStr, String Category1, String Category2) {
ItemStr = ItemStr.replace("}","");
ItemStr = ItemStr.replace("{","");
int loc1 = ItemStr.indexOf((Category1));
int loc2 = 0;
if (!Category2.equals("")) {
loc2 = ItemStr.indexOf((Category2));
} else {
loc2 = ItemStr.length();
}
String shortStr = ItemStr.substring(loc1,loc2);
shortStr = shortStr.replace(Category1,"").trim();
int len = shortStr.length();
if (len > 0){
if (shortStr.substring(len-1,len).equals(",")) {
shortStr = shortStr.substring(0,Math.max(0,len-1));
}
}
return shortStr;
}
}
我可以看到代码执行, GetView()按预期执行。
首先,当最初创建ListView(没有选择特定的项目)时,单个项目的背景颜色不是按预期透明,而是灰色。那不对。
然后,当选择一个项目时,GetView()代码部分 selectedPos == position 按预期执行。
但没有背景颜色改变它的结果。
我是否完全搞砸了或者我错过了一些事情?
答案 0 :(得分:0)
尝试使用setActivated。
3|2|33|Credit card number is required.||P|0|||0.00|CC|auth_capture||||||||||||||||||||||||||THISISANALPHANUMERICNUMBER||||||||||||||||||||||||||||||
然后只需激活和停用
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@color/amber_800" />
<item android:state_pressed="true" android:drawable="@color/itemPressed" />
<item android:drawable="@color/transparent" />
</selector>
答案 1 :(得分:0)
我做了一些WAG(Wild A ** Guesses)并找到答案。
问题出在 getView() BackgroundColor参数中。
if (selectedPos == position) {
v.setSelected(true);
v.setPressed(true);
v.setBackgroundColor(R.color.amber_800);
} else {
v.setSelected(false);
v.setPressed(false);
v.setBackgroundColor(R.color.text_white);
}
当我尝试使用资源定义的颜色时,例如: v.setBackgroundColor(R.color.amber_800); 它不起作用。
事实上,有时候 R.color.amber_800 在红色下划线(表示问题)。令人困惑的部分是,很多时候它没有用红色标出,有时也是如此。
无论如何,当我将这些颜色参数更改为:
v.setBackgroundColor(Color.YELLOW);
and
v.setBackgroundColor(Color.TRANSPARENT);
事情开始奏效了。
有谁知道如何让资源颜色设置按预期工作?