3

我正在尝试为列表视图设置替代背景颜色,并在每次启动该活动时随机设置背景颜色。

在此处输入图像描述 在此处输入图像描述

阵列适配器

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>
4

3 回答 3

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]);
于 2012-12-21T13:16:36.177 回答
2

在getview方法中,可以实现一个逻辑,即如果位置是偶数则设置背景色1,如果位置是奇数则设置背景色2。

于 2012-12-21T13:19:54.933 回答
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();它会请求具有先前位置背景颜色的视图。这就是为什么你得到那个输出

于 2012-12-22T05:38:51.133 回答