如何将图像添加到AdapterClass中的RecyclerView?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将图像添加到AdapterClass中的RecyclerView?相关的知识,希望对你有一定的参考价值。
这是Product bean类enter image description here
所以我的Recycle视图正在运行,但是这行代码没有用。这来自My Adapter Class。
holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(product.getImage()));
类
@NonNull
@Override
public productViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.list_layout,null);
productViewHolder holder = new productViewHolder(view);
return holder;
// return new productViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull productViewHolder holder, int position) {
Product product = productList.get(position);
holder.textViewTitle.setText(product.getTitle());
holder.textViewDescription.setText(product.getShort_description());
holder.textViewRating.setText(String.valueOf(product.getRating()));
holder.textViewPrice.setText(String.valueOf(product.getPrice()));
// holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(product.getImage()));
}
@Override
public int getItemCount() {
//returns number of elements within the list
return productList.size();
}
class productViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView textViewTitle, textViewDescription, textViewRating, textViewPrice;
public productViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
textViewTitle = itemView.findViewById(R.id.textViewTitle);
textViewDescription = itemView.findViewById(R.id.textViewShortDesc);
textViewRating = itemView.findViewById(R.id.textViewRating);
textViewPrice = itemView.findViewById(R.id.textViewPrice);
}
如果产品对象中的图像包含可绘制包中的图像资源,则需要使用
holder.imageView.setImageResource(product.getImage());
试试这个:
如果您获取数据表单api设置图像这样
RequestOptions options = new RequestOptions()
.centerCrop()
.placeholder(R.drawable.avatar1)
.error(R.drawable.avatar1)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.priority(Priority.HIGH)
.dontAnimate()
.dontTransform();
Glide.with(context)
.load(UpcomingHistoryList.get(position).getPhotoUrl())
.apply(options)
.into(holder.img);
你可以使用滑翔和毕加索
如果你得到图像形式drawable设置像这样的图像
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageResource(R.drawable.hello);
我认为你得到了一个产品对象的String,你应该尝试将该estring转换为Uri或bitmap,这样当你在imageview中创建一个set然后你可以展示时,如果你展示你的产品类知道它会有很大帮助什么类型的值返回product.getimage(); Uri.parse(product.getImage());
有一种方法可用于加载可绘制文件,它们是vector isset
public static BitmapDescriptor getBitmapFromVector(@NonNull Context context,
@DrawableRes int vectorResourceId,
@ColorInt int tintColor) {
Drawable vectorDrawable = ResourcesCompat.getDrawable(
context.getResources(), vectorResourceId, null);
if (vectorDrawable == null) {
String TAG = "tag";
Log.e(TAG, "Requested vector resource was not found");
return BitmapDescriptorFactory.defaultMarker();
}
Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
DrawableCompat.setTint(vectorDrawable, tintColor);
vectorDrawable.draw(canvas);
return BitmapDescriptorFactory.fromBitmap(bitmap);
}
之后,它只剩下imageview.setImageBitmap(Bitmapdescriptor(参数);
通过阅读注释,您的产品对象看起来像将图像存储为字符串。 getImage()方法必须返回int。
在Product中声明图像(带有一些默认图像),如下所示:
private int image = R.drawable.default_image;
吸气剂看起来像这样
public int getImage() {
return image;
}
// Object class for Product
class Product{
public Product(String title, String short_description, double rating, double price, int image) {
this.title = title;
this.short_description = short_description;
this.rating = rating;
this.price = price;
this.image = image;
}
String title;
String short_description;
double rating;
double price;
int image;
}
//Array List of products
List<Product> productList = new ArrayList<>();
productList.add(new Product("Title1", "Short Description1", 4, 50, R.drawable.ic_thumb_down));
productList.add(new Product("Title2", "Short Description2", 3, 100, R.drawable.ic_thumb_down));
productList.add(new Product("Title3", "Short Description3", 4, 150, R.drawable.ic_thumb_down));
//Adapter
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder>{
List<Integer> resourceList;
public ImageAdapter(List<Integer> resourceList) {
this.resourceList = resourceList;
}
@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new ImageViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_image, viewGroup, false));
}
@Override
public void onBindViewHolder(@NonNull ImageViewHolder imageViewHolder, int i) {
Product product = productList.get(i);
imageViewHolder.imageView.setImageResource(product.image);
// Here you can also use other field like title, description, etc...
}
@Override
public int getItemCount() {
return resourceList.size();
}
class ImageViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
public ImageViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView3);
}
}
}
尝试使用:
holder.imageView.setImageDrawable(ContextCompact.getDrawable(ctxt, R.drawable.YOUR_RESOURCE_IMAGE));
如果你的product.getimage()返回一个资源ID,它是R.drawable.YOUR_RESOURCE_IMAGE,请尝试使用:
holder.imageView.setImageDrawable(ContextCompact.getDrawable(ctxt, product.getimage()));
以上是关于如何将图像添加到AdapterClass中的RecyclerView?的主要内容,如果未能解决你的问题,请参考以下文章