package com.datarecovery.master.module.imgrecover; import android.content.Context; import android.view.LayoutInflater; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.lifecycle.LifecycleOwner; import androidx.recyclerview.widget.RecyclerView; import com.atmob.common.logging.AtmobLog; import com.datarecovery.master.databinding.ItemDataImgBinding; import com.datarecovery.master.sdk.bugly.BuglyHelper; import com.datarecovery.master.utils.ImageDeepDetector; import java.util.List; public class ImageItemAdapter extends RecyclerView.Adapter { private List list; private final LifecycleOwner lifecycleOwner; private final String type; private int size; private onItemClick onItemClick; public ImageItemAdapter(String type, LifecycleOwner lifecycleOwner) { this.type = type; this.lifecycleOwner = lifecycleOwner; } public void setOnItemClick(ImageItemAdapter.onItemClick onItemClick) { this.onItemClick = onItemClick; } @NonNull @Override public ImageItemAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { Context context = parent.getContext(); LayoutInflater layoutInflater = LayoutInflater.from(context); ItemDataImgBinding binding = ItemDataImgBinding.inflate(layoutInflater, parent, false); return new ImageItemAdapter.ViewHolder(binding); } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { if (position < 0 || position >= list.size()) { BuglyHelper.postCatchedException(new IndexOutOfBoundsException("图片列表onBindViewHolder出现数组越界:type:" + type + "," + position + ",size:" + list.size())); return; } holder.bind(list.get(position)); } public void submit(List imageList) { if (imageList == null) { return; } int itemCount; if (list != null) { itemCount = imageList.size() - size; } else { itemCount = imageList.size(); } this.size = imageList.size(); this.list = imageList; if (itemCount >= 0) { notifyItemRangeInserted(0, itemCount); } } @Override public int getItemCount() { return (list == null) ? 0 : list.size(); } public class ViewHolder extends RecyclerView.ViewHolder { ItemDataImgBinding binding; public ViewHolder(@NonNull ItemDataImgBinding binding) { super(binding.getRoot()); this.binding = binding; binding.setLifecycleOwner(lifecycleOwner); binding.setCheckBoxClick(v -> { if (onItemClick != null) { onItemClick.onCheck(binding.getFile()); } }); binding.getRoot().setOnClickListener(v -> { if (onItemClick != null) { onItemClick.onImgItemClick(binding.getFile()); } }); } public void bind(ImageDeepDetector.ImageFile imageFile) { binding.setFile(imageFile); } } public interface onItemClick { void onCheck(ImageDeepDetector.ImageFile imageFile); void onImgItemClick(ImageDeepDetector.ImageFile imageFile); } }