| | |
| | | package com.dayu.pipirrapp.adapter; |
| | | |
| | | import android.content.Context; |
| | | import android.view.LayoutInflater; |
| | | import android.view.View; |
| | | import android.view.ViewGroup; |
| | |
| | | import androidx.recyclerview.widget.RecyclerView; |
| | | |
| | | import com.bumptech.glide.Glide; |
| | | import com.bumptech.glide.load.resource.bitmap.CenterCrop; |
| | | import com.bumptech.glide.load.resource.bitmap.RoundedCorners; |
| | | import com.dayu.pipirrapp.R; |
| | | import com.dayu.pipirrapp.bean.net.ImageResult; |
| | | import com.dayu.pipirrapp.tool.GlideEngine; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * ImageAdapter - |
| | |
| | | * @version 1.0 |
| | | * @since 2024-11-27 |
| | | */ |
| | | public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder>{ |
| | | private String[] imageUrls; // 图片的 URL 或本地路径 |
| | | public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> { |
| | | private List<ImageResult> imageUrls; // 图片的 URL 或本地路径 |
| | | private Context mContext; |
| | | private OnItemClickListener onItemClickListener; |
| | | |
| | | // 构造方法 |
| | | public ImageAdapter(String[] imageUrls) { |
| | | public ImageAdapter(Context context, List<ImageResult> imageUrls, OnItemClickListener onItemClickListener) { |
| | | this.imageUrls = imageUrls; |
| | | this.mContext = context; |
| | | this.onItemClickListener = onItemClickListener; |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | @Override |
| | | public void onBindViewHolder(ImageViewHolder holder, int position) { |
| | | |
| | | // 使用 Glide 加载图片到 ImageView 中 |
| | | Glide.with(holder.itemView.getContext()) |
| | | .load(imageUrls[position]) // 加载图片的 URL 或路径 |
| | | .thumbnail(0.1f) // 设置缩略图比例 |
| | | .load(imageUrls.get(position).getWebPath()) // 加载图片的 URL 或路径 |
| | | .thumbnail(0.1f) // 设置缩略图比例' |
| | | .transform(new CenterCrop(), new RoundedCorners(8)) |
| | | .placeholder(R.drawable.ps_image_placeholder) |
| | | .into(holder.imageView); |
| | | holder.imageView.setOnClickListener(v -> { |
| | | onItemClickListener.onItemClick(v, position); |
| | | }); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public int getItemCount() { |
| | | return imageUrls.length; |
| | | if (imageUrls == null) { |
| | | return 0; |
| | | } |
| | | return imageUrls.size(); |
| | | } |
| | | |
| | | public static class ImageViewHolder extends RecyclerView.ViewHolder { |
| | |
| | | |
| | | public ImageViewHolder(View itemView) { |
| | | super(itemView); |
| | | // imageView = itemView.findViewById(R.id.thumbnailImageView); |
| | | imageView = itemView.findViewById(R.id.thumbnailImageView); |
| | | } |
| | | } |
| | | |
| | | public interface OnItemClickListener { |
| | | /** |
| | | * Item click event |
| | | * |
| | | * @param v |
| | | * @param position |
| | | */ |
| | | void onItemClick(View v, int position); |
| | | |
| | | } |
| | | |
| | | |
| | | } |