图片适配器代码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" />
posted on 2024-06-28 22:00  一点都不难  阅读(11)  评论(0编辑  收藏  举报