首页 > 解决方案 > 如何将空图像添加到 RecyclerView

问题描述

我正在使用 RecyclerView,我不想在我的某些 ViewHolders 中使用图像。

int[] images = {R.drawable.image1, R.drawable.image2};

我在一个 int 数组中传递我的图像。是否可以在此数组中传入空白图像/空图像,以便图像不会呈现?

正如@muhammad-ahmed 所建议的,我已经编辑了我的整数数组并将其添加到我的 onBindViewHolder 函数中。

Integer[] images = {null, R.drawable.image2};

public void onBindViewHolder(@NonNull thisViewHolder holder, int position) {

    if (images_data[position] == null) {
        holder.image.setVisibility(View.GONE);
    } else {
        holder.image.setImageResource(post_images_data[position]);
    }
}

标签: androidxmlandroid-layout

解决方案


使用参考类型/框类型

Integer[] post_images = {R.drawable.animal_0, R.drawable.animal_1,
                     null, R.drawable.animal_3};

并在 RecyclerView ViewHolder 检查图像是否为空隐藏 ImageView


推荐阅读