为ViewHolder中的每个项目添加不同的背景颜色

时间:2016-02-05 22:26:31

标签: android gridview background android-viewholder

我使用ViewHolder适配器创建了一个GridView,在每个项目中包含一个图像和一些文本 代码正常工作,显示顺畅,但现在我想为每个项目添加不同的背景颜色。我检查了几个帖子,例如this onethis one,但我无法让它发挥作用。

GridViewAdapter在Main Activity

中创建
private List<GridViewItem> mItems;    // GridView items list
private GridViewAdapter mAdapter;    // GridView adapter

并在onCreate中使用如下:

mItems = new ArrayList<GridViewItem>();
Resources resources = getResources();

mItems.add(new GridViewItem(resources.getDrawable(R.drawable.truck_package), getString(R.string.drop_package)));
mItems.add(new GridViewItem(resources.getDrawable(R.drawable.man_package), getString(R.string.pick_package)));
mItems.add(new GridViewItem(resources.getDrawable(R.drawable.register), getString(R.string.register)));
mAdapter = new GridViewAdapter(this, mItems);
// initialize the GridView
final GridView gridView = (GridView) this.findViewById(R.id.gridView);
gridView.setAdapter(mAdapter);

我的GridViewAdapter的工作原理如下:

public class GridViewAdapter extends BaseAdapter {
    private Context mContext;
    private List<GridViewItem> mItems;


    public GridViewAdapter(Context context, List<GridViewItem> items) {
        mContext = context;
        mItems = items;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if(convertView == null) {
            // inflate the GridView item layout
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.gridview_item, parent, false);

            // initialize the view holder
            viewHolder = new ViewHolder();
            viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
            convertView.setTag(viewHolder);
        } else {
            // recycle the already inflated view
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // update the item view
        GridViewItem item = mItems.get(position);
        viewHolder.ivIcon.setImageDrawable(item.icon);
        viewHolder.tvTitle.setText(item.title);

        return convertView;
    }

    /**
     * The view holder design pattern prevents using findViewById()
     * repeatedly in the getView() method of the adapter.
     *
     * @see ://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder
     */
    private static class ViewHolder {
        ImageView ivIcon;
        TextView tvTitle;
    }
}

最后,GridView的每个项目定义如下:

public class GridViewItem {
    public final Drawable icon;       // the drawable for the ListView item ImageView
    public final String title;        // the text for the GridView item title


    public GridViewItem(Drawable icon, String title) {
        this.icon = icon;
        this.title = title;

    }
}

我的猜测是我应该添加一个&#34;颜色&#34; GridViewItem中的变量。然后在文本和图片的viewHolder.itemView.setBackgroundColor(color)中使用GridViewAdapter。你认为这是正确的方法吗?如果是这样,你会怎么做?

最后,我不完全确定这是否是使用gridView的最佳方式,所以我很想知道是否有更好的方法来实现它。谢谢!

1 个答案:

答案 0 :(得分:1)

使用R.layout.gridview_item文件的父布局ID。假设您的行布局是该文件中的父布局,然后使用以下方式。

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if(convertView == null) {
            // inflate the GridView item layout
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.gridview_item, parent, false);

            // initialize the view holder
            viewHolder = new ViewHolder();
            viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
            viewHolder.line_list_item = (LinearLayout) convertView.findViewById(R.id.line_list_item);
            convertView.setTag(viewHolder);
        } else {
            // recycle the already inflated view
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // update the item view
        GridViewItem item = mItems.get(position);
        viewHolder.ivIcon.setImageDrawable(item.icon);
        viewHolder.tvTitle.setText(item.title);
        if (position % 2 == 0) {
                viewHolder.line_main.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorPrimaryDark));
            } else {
                viewHolder.line_main.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
            }

        return convertView;
    }