为 ViewHolder 中的每个项目放置不同的背景颜色

Put different background colors for each item in a ViewHolder

我使用 ViewHolder Adapter 创建了一个 GridView,以便在每个项目中包含一个图像和一些文本。
代码工作正常并且显示流畅,但现在我想为每个项目添加不同的背景颜色。我检查了几个帖子,比如 or this 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;

    }
}

我的猜测是我应该在 GridViewItem 中添加一个 "color" 变量。然后根据文本和图像将其与 GridViewAdapter 中的 viewHolder.itemView.setBackgroundColor(color) 一起使用。你认为这是正确的做法吗?如果是,你会怎么做?

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

使用 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;
    }