android - 如何将空图像添加到 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]);
}
}
解决方案
使用参考类型/框类型
Integer[] post_images = {R.drawable.animal_0, R.drawable.animal_1,
null, R.drawable.animal_3};
并在 RecyclerView ViewHolder 检查图像是否为空隐藏 ImageView
推荐阅读
- android - Leak Canary 检测到 Kotlin Singleton 泄漏内存
- ruby-on-rails - 哈希参数[:id] before_Action
- github - 如何设置 https://cla.opensource.microsoft.com 的设置
- java - Java Write to File Perfomance
- css - 将文本对齐到 Snackbar 内的中心(角度材质)
- go - 如何查看我对在 Heroku 本地运行的 Go 应用程序所做的更改?
- node.js - 检查用户 ID 是否在 mongoose 文档中 - 如果是,将属性值更改为 true?
- rest - Spring Boot:考虑用户角色,谁来编写自定义 Prefilter?
- javascript - 色轮选择器光标
- javascript - 以 http:// 或 https:// 开头的 URL,以逗号分隔,没有空格