2012-12-21 109 views
3

我想爲列表視圖設置備選背景顏色,並隨機設置背景顏色,每次我開始該活動。ListView的Android替代顏色是隨機

enter image description here enter image description here

數組適配器

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> 
+2

在返回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;請讓我知道 –

+0

@nick它完成了這項工作。任何想法爲什麼發生問題? –

+1

我對此沒有證實,但在答案中寫下了我認爲的邏輯 –

回答

1

在返回convertView之前,請根據需要設置背景。 剛剛返回convertView

if (position % 2 == 0) 
{ 
convertView .setBackgroundResource(R.drawable.list_view_place_row_1); 
} 
else 
{ 
convertView .setBackgroundResource(R.drawable.list_view_place_row_2); 
} 
return convertView; 

之前,使用這條線我不知道隊友,,,但我認爲它會去(convertView == null)內首次。之後,當你撥打holder = (ViewHolder) convertView.getTag();那麼它請求的視圖,其中有以前的位置背景顏色。這就是爲什麼你得到那個輸出

+0

如果它似乎對你有幫助,那麼你可以接受答案 –

3

代碼似乎是正確的。可嘗試以下操作:顏色

申報整數數組:

private int[] colors = new int[] { R.drawable.list_view_place_row_1, R.drawable.drawable.list_view_place_row_2}; 

而且在getView()....

int colorPos = position % colors.length; 
convertView.setBackgroundResource(colors[colorPos]); 
+0

尼克斯回答完成了這項工作。想知道爲什麼垃圾價值:( –

2
在getview方法

,你可以實現一個邏輯,即如果位置是偶數,則設置背景顏色1,如果位置是奇數,則設置背景顏色2。