我想爲列表視圖設置備選背景顏色,並隨機設置背景顏色,每次我開始該活動。ListView的Android替代顏色是隨機
數組適配器
public class PlacesListAdapter extends ArrayAdapter<Place> implements
Filterable {
public Context context;
private List<Place> orig, itemDetailsrrayList;
private PlaceFilter filter;
public PlacesListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public PlacesListAdapter(Context context, int resource, List<Place> places) {
super(context, resource, places);
this.context = context;
// this.places = places;
itemDetailsrrayList = places;
orig = new ArrayList<Place>(itemDetailsrrayList);
filter = new PlaceFilter();
// imageLoader = new ImageLoader(context.getApplicationContext());
}
public int getCount() {
return itemDetailsrrayList.size();
}
public Place getItem(int position) {
return itemDetailsrrayList.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
// View view = convertView;
// Place p = places.get(position);
if (convertView == null) {
LayoutInflater viewInflater;
viewInflater = LayoutInflater.from(getContext());
convertView = viewInflater.inflate(R.layout.list_item_place, null);
holder = new ViewHolder();
holder.placeTitle = (TextView) convertView
.findViewById(R.id.place_title);
holder.placeDistance = (TextView) convertView
.findViewById(R.id.place_distance);
holder.placeCategoryIcon = (ImageView) convertView
.findViewById(R.id.place_category_icon);
// Setting Alternative Row Colors
if (position % 2 == 0) {
convertView
.setBackgroundResource(R.drawable.list_view_place_row_1);
} else {
convertView
.setBackgroundResource(R.drawable.list_view_place_row_2);
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.placeTitle.setText(itemDetailsrrayList.get(position)
.getPlaceTitle());
holder.placeDistance.setText("200");
holder.placeCategoryIcon.setImageResource(R.drawable.icon_category);
return convertView;
}
static class ViewHolder {
TextView placeId;
TextView placeTitle;
TextView placeDistance;
ImageView placeCategoryIcon;
}
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
return filter;
}
private class PlaceFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults oReturn = new FilterResults();
ArrayList<Place> results = new ArrayList<Place>();
if (orig == null)
orig = itemDetailsrrayList;
if (constraint != null) {
if (orig != null && orig.size() > 0) {
for (Place g : orig) {
if (g.getPlaceTitle()
.toLowerCase()
.startsWith(constraint.toString().toLowerCase()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
itemDetailsrrayList = (ArrayList<Place>) results.values;
notifyDataSetChanged();
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/list_view_place_row_1" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@color/list_view_place_selected" android:state_pressed="true" android:state_selected="false"/>
<item android:drawable="@color/list_view_place_selected" android:state_pressed="false" android:state_selected="true"/>
<item android:drawable="@android:color/transparent"/>
</selector>
在返回convertView之前,請根據需要設置背景。 使用此線剛返回之前convertView 如果(位置%2 == 0){ convertView .setBackgroundResource(R.drawable.list_view_place_row_1); } else { convertView .setBackgroundResource(R.drawable.list_view_place_row_2); } return convertView;請讓我知道 –
@nick它完成了這項工作。任何想法爲什麼發生問題? –
我對此沒有證實,但在答案中寫下了我認爲的邏輯 –