diff --git a/commonLib/src/main/java/com/common/commonlib/bean/GridPicBean.kt b/commonLib/src/main/java/com/common/commonlib/bean/GridPicBean.kt new file mode 100644 index 0000000..dad0409 --- /dev/null +++ b/commonLib/src/main/java/com/common/commonlib/bean/GridPicBean.kt @@ -0,0 +1,3 @@ +package com.common.commonlib.bean + +data class GridPicBean(val imagePath:String, val isConverting: Boolean) diff --git a/commonLib/src/main/java/com/common/commonlib/view/adapter/PicGridAdapter.java b/commonLib/src/main/java/com/common/commonlib/view/adapter/PicGridAdapter.java new file mode 100644 index 0000000..3393ee9 --- /dev/null +++ b/commonLib/src/main/java/com/common/commonlib/view/adapter/PicGridAdapter.java @@ -0,0 +1,164 @@ +package com.common.commonlib.view.adapter; + +import android.content.Context; +import android.graphics.drawable.AnimatedVectorDrawable; +import android.os.Build; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import com.bumptech.glide.Glide; +import com.bumptech.glide.load.MultiTransformation; +import com.bumptech.glide.load.resource.bitmap.CenterCrop; +import com.bumptech.glide.load.resource.bitmap.RoundedCorners; +import com.common.commonlib.R; +import com.common.commonlib.bean.GridPicBean; + +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; + +public class PicGridAdapter extends RecyclerView.Adapter { + private static final int TYPE_ONE = 0; + private static final int TYPE_TWO = 1; + private final List gridPicBeanList = new ArrayList<>(); + private Context mContext; + private onAddClickListener onAddClickListener; + + public PicGridAdapter(Context context) { + mContext = context; + } + + @Override + public int getItemViewType(int position) { + if (position == gridPicBeanList.size()) { + return TYPE_ONE; + } else { + return TYPE_TWO; + } + } + + @NotNull + @Override + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + RecyclerView.ViewHolder viewHolder; + if (viewType == TYPE_ONE) { + View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_grid_pic_add, parent, false); + viewHolder = new AddIconViewHolder(inflate); + } else { + View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_grid_pic_show, parent, false); + viewHolder = new PicViewHolder(inflate); + } + return viewHolder; + } + + @Override + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { + if (position < gridPicBeanList.size()) { + if (holder instanceof PicViewHolder) { + PicViewHolder viewHolder = (PicViewHolder) holder; + GridPicBean gridPicBean = gridPicBeanList.get(position); + Glide.with(mContext).load(gridPicBean.getImagePath()) + .transform(new MultiTransformation(new CenterCrop(), new RoundedCorners(5))) + .into(viewHolder.imageView); + viewHolder.imageDeleteView.setOnClickListener(l -> { + gridPicBeanList.remove(holder.getAdapterPosition()); + notifyItemRemoved(holder.getAdapterPosition()); + }); + + AnimatedVectorDrawable drawable = (AnimatedVectorDrawable) viewHolder.ivLoading.getDrawable(); + if (gridPicBean.isConverting()) { + viewHolder.ivLoading.setVisibility(View.VISIBLE); + viewHolder.imageDeleteView.setVisibility(View.GONE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + drawable.reset(); + } + drawable.start(); + + } else { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + drawable.reset(); + } + viewHolder.ivLoading.setVisibility(View.GONE); + viewHolder.imageDeleteView.setVisibility(View.VISIBLE); + } + } + } else { + if (holder instanceof AddIconViewHolder) { + AddIconViewHolder viewHolder = (AddIconViewHolder) holder; + viewHolder.imageView.setOnClickListener(v -> { + if (gridPicBeanList.size() < 4) { + if (onAddClickListener != null) { + onAddClickListener.onClick(viewHolder.getAdapterPosition()); + } + } else { + Toast.makeText(mContext, "最多添加四张!", Toast.LENGTH_SHORT).show(); + } + }); + } + } + } + + @Override + public int getItemCount() { + return gridPicBeanList.size() + 1; + } + + public void setOnAddClickListener(PicGridAdapter.onAddClickListener onAddClickListener) { + this.onAddClickListener = onAddClickListener; + } + + public void addPicData(GridPicBean gridPicBean) { + gridPicBeanList.add(gridPicBean); + notifyItemRangeChanged(gridPicBeanList.size() - 1, 2); + } + + public void clearPicData() { + gridPicBeanList.clear(); + } + + public void replaceData(int position, GridPicBean gridPicBean) { + gridPicBeanList.set(position, gridPicBean); + notifyDataSetChanged(); + } + + public List getImagePathList() { + List images = new ArrayList<>(); + for (GridPicBean gridPicBean : gridPicBeanList) { + images.add(gridPicBean.getImagePath()); + } + return images; + } + + interface onAddClickListener { + void onClick(int position); + } + + private static class AddIconViewHolder extends RecyclerView.ViewHolder { + private ImageView imageView; + + public AddIconViewHolder(@NonNull @NotNull View itemView) { + super(itemView); + imageView = (ImageView) itemView.findViewById(R.id.bg_pic_add); + } + } + + private static class PicViewHolder extends RecyclerView.ViewHolder { + private ImageView imageView; + private ImageView imageDeleteView; + private ImageView ivLoading; + + public PicViewHolder(@NonNull @NotNull View itemView) { + super(itemView); + imageView = itemView.findViewById(R.id.pic_iv); + imageDeleteView = itemView.findViewById(R.id.pic_delete_iv); + ivLoading = itemView.findViewById(R.id.iv_loading); + } + } +} diff --git a/commonLib/src/main/res/layout/item_grid_pic_add.xml b/commonLib/src/main/res/layout/item_grid_pic_add.xml new file mode 100644 index 0000000..0d12457 --- /dev/null +++ b/commonLib/src/main/res/layout/item_grid_pic_add.xml @@ -0,0 +1,14 @@ + + + + + + diff --git a/commonLib/src/main/res/layout/item_grid_pic_show.xml b/commonLib/src/main/res/layout/item_grid_pic_show.xml new file mode 100644 index 0000000..ff7f15f --- /dev/null +++ b/commonLib/src/main/res/layout/item_grid_pic_show.xml @@ -0,0 +1,31 @@ + + + + + + + + +