| | |
| | | |
| | | import android.content.Context; |
| | | import android.net.Uri; |
| | | import android.text.TextUtils; |
| | | import android.view.LayoutInflater; |
| | | import android.view.View; |
| | | import android.view.ViewGroup; |
| | | import android.widget.ImageView; |
| | | import android.widget.RelativeLayout; |
| | | import android.widget.TextView; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.recyclerview.widget.RecyclerView; |
| | | |
| | | import com.bumptech.glide.Glide; |
| | | import com.bumptech.glide.load.engine.DiskCacheStrategy; |
| | | import com.dayu.pipirrapp.R; |
| | | import com.dayu.pipirrapp.bean.net.UplodFileState; |
| | | import com.dayu.pipirrapp.listener.OnItemLongClickListener; |
| | | import com.luck.picture.lib.adapter.holder.PreviewGalleryAdapter; |
| | | import com.dayu.pipirrapp.utils.MyLog; |
| | | import com.king.view.circleprogressview.CircleProgressView; |
| | | import com.luck.picture.lib.config.PictureMimeType; |
| | | import com.luck.picture.lib.config.SelectMimeType; |
| | | import com.luck.picture.lib.entity.LocalMedia; |
| | |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * AddPictureAdapter - |
| | |
| | | public static final int TYPE_PICTURE = 2; |
| | | private final LayoutInflater mInflater; |
| | | private final ArrayList<LocalMedia> list = new ArrayList<>(); |
| | | Map<String, UplodFileState> fileStates; |
| | | private int selectMax = 9; |
| | | private final Context mContext; |
| | | |
| | | /** |
| | | * 删除 |
| | | */ |
| | | public void delete(int position) { |
| | | try { |
| | | |
| | | if (position != RecyclerView.NO_POSITION && list.size() > position) { |
| | | list.remove(position); |
| | | notifyItemRemoved(position); |
| | | notifyItemRangeChanged(position, list.size()); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | public AddPictureAdapter(Context context, List<LocalMedia> result) { |
| | | public AddPictureAdapter(Context context, List<LocalMedia> result, Map<String, UplodFileState> fileStates) { |
| | | this.mInflater = LayoutInflater.from(context); |
| | | this.list.addAll(result); |
| | | this.mContext = context; |
| | | this.fileStates = fileStates; |
| | | } |
| | | |
| | | public void setSelectMax(int selectMax) { |
| | |
| | | } |
| | | } |
| | | |
| | | public static class ViewHolder extends RecyclerView.ViewHolder { |
| | | |
| | | ImageView mImg; |
| | | ImageView mIvDel; |
| | | TextView tvDuration; |
| | | |
| | | public ViewHolder(View view) { |
| | | super(view); |
| | | mImg = view.findViewById(R.id.fiv); |
| | | mIvDel = view.findViewById(R.id.iv_del); |
| | | tvDuration = view.findViewById(R.id.tv_duration); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public int getItemCount() { |
| | |
| | | /** |
| | | * 创建ViewHolder |
| | | */ |
| | | @NonNull |
| | | @Override |
| | | public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { |
| | | View view = mInflater.inflate(R.layout.item_add_filter_image, viewGroup, false); |
| | |
| | | * 设置值 |
| | | */ |
| | | @Override |
| | | public void onBindViewHolder(final ViewHolder viewHolder, final int position) { |
| | | public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) { |
| | | //少于MaxSize张,显示继续添加的图标 |
| | | if (getItemViewType(position) == TYPE_CAMERA) { |
| | | viewHolder.mImg.setImageResource(R.mipmap.ic_add_image); |
| | | viewHolder.mImg.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View view) { |
| | | if (mItemClickListener != null) { |
| | | mItemClickListener.openPicture(); |
| | | } |
| | | viewHolder.mImg.setOnClickListener(view -> { |
| | | if (mItemClickListener != null) { |
| | | mItemClickListener.openPicture(); |
| | | } |
| | | }); |
| | | viewHolder.mIvDel.setVisibility(View.INVISIBLE); |
| | | viewHolder.circleProgressView.setVisibility(View.GONE); |
| | | viewHolder.maskLayerBg.setVisibility(View.GONE); |
| | | } else { |
| | | //删除图片 |
| | | viewHolder.mIvDel.setVisibility(View.VISIBLE); |
| | | viewHolder.mIvDel.setOnClickListener(view -> { |
| | | int index = viewHolder.getAbsoluteAdapterPosition(); |
| | | if (index != RecyclerView.NO_POSITION && list.size() > index) { |
| | | mItemClickListener.onDeleteClick(index); |
| | | list.remove(index); |
| | | notifyItemRemoved(index); |
| | | notifyItemRangeChanged(index, list.size()); |
| | |
| | | .centerCrop() |
| | | .placeholder(R.color.app_color_f6) |
| | | .diskCacheStrategy(DiskCacheStrategy.ALL) |
| | | .thumbnail(0.25f) |
| | | .into(viewHolder.mImg); |
| | | } |
| | | |
| | | // 更新进度 |
| | | UplodFileState uplodFileState = TextUtils.isEmpty(media.getCompressPath()) |
| | | ? fileStates.get(media.getRealPath()) |
| | | : fileStates.get(media.getCompressPath()); |
| | | if (uplodFileState != null) { |
| | | MyLog.d("progressRequestBodyHolder>>>" + "path:" + uplodFileState.getFilePath() + ">>>position:" + position); |
| | | uplodFileState.setAdapterPosition(position); |
| | | MyLog.d("progressRequestBodyHolder>>>" + "State:" + uplodFileState.getState() + ">>>position:" + position + ">>>Progress: " + uplodFileState.getProgress()); |
| | | |
| | | if (uplodFileState.getState() == UplodFileState.STATE_UPDING) { |
| | | MyLog.d("progressRequestBodyHolder>>>" + "正在上传"); |
| | | viewHolder.postError.setVisibility(View.GONE); |
| | | if (uplodFileState.getProgress() < 100) { |
| | | viewHolder.maskLayerBg.setVisibility(View.VISIBLE); |
| | | viewHolder.circleProgressView.setVisibility(View.VISIBLE); |
| | | viewHolder.circleProgressView.setProgress(uplodFileState.getProgress()); |
| | | } else { |
| | | viewHolder.circleProgressView.setVisibility(View.GONE); |
| | | viewHolder.maskLayerBg.setVisibility(View.GONE); |
| | | } |
| | | } else if (uplodFileState.getState() == UplodFileState.STATE_DONE) { |
| | | MyLog.d("progressRequestBodyHolder>>>" + "完成"); |
| | | viewHolder.maskLayerBg.setVisibility(View.GONE); |
| | | viewHolder.circleProgressView.setVisibility(View.GONE); |
| | | } else { |
| | | MyLog.d("progressRequestBodyHolder>>>" + "错误"); |
| | | viewHolder.maskLayerBg.setVisibility(View.VISIBLE); |
| | | viewHolder.postError.setVisibility(View.VISIBLE); |
| | | viewHolder.circleProgressView.setVisibility(View.GONE); |
| | | } |
| | | } |
| | | |
| | | |
| | | //itemView 的点击事件 |
| | | if (mItemClickListener != null) { |
| | | viewHolder.itemView.setOnClickListener(v -> { |
| | | int adapterPosition = viewHolder.getAbsoluteAdapterPosition(); |
| | | mItemClickListener.onItemClick(v, adapterPosition); |
| | | }); |
| | | viewHolder.postError.setOnClickListener(v -> { |
| | | mItemClickListener.onReUpload(uplodFileState); |
| | | }); |
| | | } |
| | | |
| | | if (mItemLongClickListener != null) { |
| | | viewHolder.itemView.setOnLongClickListener(v -> { |
| | | int adapterPosition = viewHolder.getAbsoluteAdapterPosition(); |
| | | int adapterPosition = viewHolder.getBindingAdapterPosition(); |
| | | mItemLongClickListener.onItemLongClick(viewHolder, adapterPosition, v); |
| | | return true; |
| | | }); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 更新进度 |
| | | * |
| | | * @param uplodFileState |
| | | */ |
| | | public void updateProgress(UplodFileState uplodFileState) { |
| | | MyLog.d("updateProgress>>>" + "State:" + uplodFileState.getState() + ">>>position:" + uplodFileState.getAdapterPosition() + ">>>Progress: " + uplodFileState.getProgress()); |
| | | |
| | | // 更新进度参数 |
| | | if (fileStates.containsKey(uplodFileState.getFilePath())) { |
| | | // 获取已有的 UplodFileState 对象并更新进度 |
| | | UplodFileState existingState = fileStates.get(uplodFileState.getFilePath()); |
| | | int position = existingState.getAdapterPosition(); |
| | | existingState.setState(uplodFileState.getState()); |
| | | if (uplodFileState.getState() == 0) { |
| | | existingState.setState(uplodFileState.getState()); |
| | | existingState.setProgress(uplodFileState.getProgress()); // 更新进度 |
| | | // 获取进度更新项的位置 |
| | | } |
| | | if (position != RecyclerView.NO_POSITION) { |
| | | notifyItemChanged(position); |
| | | } |
| | | } |
| | | } |
| | |
| | | * Open PictureSelector |
| | | */ |
| | | void openPicture(); |
| | | |
| | | /** |
| | | * 删除了某一个 |
| | | * |
| | | * @param position |
| | | */ |
| | | void onDeleteClick(int position); |
| | | |
| | | /** |
| | | * 上传失败后重新上传 |
| | | */ |
| | | void onReUpload(UplodFileState uplodFileState); |
| | | } |
| | | |
| | | private OnItemLongClickListener mItemLongClickListener; |
| | |
| | | public void setItemLongClickListener(OnItemLongClickListener l) { |
| | | this.mItemLongClickListener = l; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 删除 |
| | | */ |
| | | public void delete(int position) { |
| | | try { |
| | | if (position != RecyclerView.NO_POSITION && list.size() > position) { |
| | | list.remove(position); |
| | | notifyItemRemoved(position); |
| | | notifyItemRangeChanged(position, list.size()); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | public static class ViewHolder extends RecyclerView.ViewHolder { |
| | | |
| | | ImageView mImg; |
| | | ImageView mIvDel; |
| | | ImageView postError; |
| | | TextView tvDuration; |
| | | RelativeLayout maskLayerBg; |
| | | CircleProgressView circleProgressView; |
| | | |
| | | |
| | | public ViewHolder(View view) { |
| | | super(view); |
| | | mImg = view.findViewById(R.id.fiv); |
| | | mIvDel = view.findViewById(R.id.iv_del); |
| | | tvDuration = view.findViewById(R.id.tv_duration); |
| | | circleProgressView = view.findViewById(R.id.cpv); |
| | | maskLayerBg = view.findViewById(R.id.mask_layer_bg); |
| | | postError = view.findViewById(R.id.postError); |
| | | } |
| | | } |
| | | |
| | | } |