2024.4.26

一点都不难發表於2024-06-28

圖片介面卡程式碼ImageAdapter.java
package com.example.photo1;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
private List imageUris;
private Context context;

public ImageAdapter(List<Uri> imageUris, Context context) {
    this.imageUris = imageUris;
    this.context = context;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.image_item, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Uri uri = imageUris.get(position);
    Glide.with(context).load(uri).into(holder.imageView);
}

@Override
public int getItemCount() {
    return imageUris.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {
    ImageView imageView;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        imageView = itemView.findViewById(R.id.imageViewItem);
    }
}

}
image_item.xml檔案

<ImageView
    android:id="@+id/imageViewItem"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:scaleType="centerCrop" />