|
|
@@ -0,0 +1,301 @@
|
|
|
+package com.benyanyi.image_grid_lib;
|
|
|
+
|
|
|
+import android.content.Intent;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.view.LayoutInflater;
|
|
|
+import android.view.View;
|
|
|
+import android.view.ViewGroup;
|
|
|
+import android.widget.ImageView;
|
|
|
+import android.widget.RelativeLayout;
|
|
|
+
|
|
|
+import androidx.annotation.IntRange;
|
|
|
+import androidx.annotation.NonNull;
|
|
|
+import androidx.annotation.Nullable;
|
|
|
+import androidx.recyclerview.widget.GridLayoutManager;
|
|
|
+import androidx.recyclerview.widget.RecyclerView;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author mylove
|
|
|
+ * @date 2021/9/27
|
|
|
+ * @email ben@yanyi.red
|
|
|
+ * @overview
|
|
|
+ */
|
|
|
+public class ImagerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
|
|
+
|
|
|
+ private List<String> mData = new ArrayList<>();
|
|
|
+ private int mMaxLength = 0;//列表最大长度
|
|
|
+ private boolean isAdd = false;//是否需要添加图片操作
|
|
|
+ private int mAddRes = R.mipmap.add_photo; //添加图片图标
|
|
|
+ private int mDefaultRes = R.mipmap.image_default;//默认图片
|
|
|
+ private int mMinShowSize = 0;//最小显示个数
|
|
|
+ private boolean mShowDelete = false;//是否需要删除图片
|
|
|
+ private int mDeleteRes = R.mipmap.delete_photo;//删除图标
|
|
|
+ private int mDeletePadding = 0;//删除图标内边距
|
|
|
+ private int mGravity = 0;//默认位于右边
|
|
|
+
|
|
|
+ private final int NONE_VIEW = 0;
|
|
|
+ private final int FOOTER_TYPE = 1;
|
|
|
+
|
|
|
+ private ImageRecycler.OnAddImgClick onAddImgClick;
|
|
|
+
|
|
|
+ public ImagerAdapter(int mMaxLength, boolean isAdd, int mAddRes, int mDefaultRes, int mMinShowSize, boolean mShowDelete, int mDeleteRes, int mDeletePadding, int mGravity) {
|
|
|
+ this.mMaxLength = mMaxLength;
|
|
|
+ this.isAdd = isAdd;
|
|
|
+ this.mAddRes = mAddRes;
|
|
|
+ this.mDefaultRes = mDefaultRes;
|
|
|
+ this.mMinShowSize = mMinShowSize;
|
|
|
+ this.mShowDelete = mShowDelete;
|
|
|
+ this.mDeleteRes = mDeleteRes;
|
|
|
+ this.mDeletePadding = mDeletePadding;
|
|
|
+ this.mGravity = mGravity;
|
|
|
+ }
|
|
|
+
|
|
|
+ @NonNull
|
|
|
+ @Override
|
|
|
+ public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
+ View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.benyanyi_item_img, parent, false);
|
|
|
+ if (viewType == FOOTER_TYPE) {
|
|
|
+ return new FooterViewHolder(view);
|
|
|
+ } else {
|
|
|
+ return new ViewHolder(view);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // <enum name="right" value="0" />
|
|
|
+// <enum name="left" value="1" />
|
|
|
+// <enum name="center" value="2" />
|
|
|
+// <enum name="top" value="3" />
|
|
|
+// <enum name="bottom" value="4" />
|
|
|
+ @Override
|
|
|
+ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
|
|
+ if (holder instanceof ViewHolder) {
|
|
|
+// if (mShowDelete) {
|
|
|
+// ((ViewHolder) holder).deleteImg.setVisibility(View.VISIBLE);
|
|
|
+// ((ViewHolder) holder).deleteImg.setImageResource(mDeleteRes);
|
|
|
+// ((ViewHolder) holder).deleteImg.setPadding(mDeletePadding, mDeletePadding, mDeletePadding, mDeletePadding);
|
|
|
+//// ((ViewHolder) holder).itemRelative
|
|
|
+// } else {
|
|
|
+// ((ViewHolder) holder).deleteImg.setVisibility(View.GONE);
|
|
|
+// }
|
|
|
+ if (mShowDelete) {
|
|
|
+ ImageView imageView = new ImageView(holder.itemView.getContext());
|
|
|
+ imageView.setImageResource(mDeleteRes);
|
|
|
+ imageView.setPadding(mDeletePadding, mDeletePadding, mDeletePadding, mDeletePadding);
|
|
|
+ RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
|
|
|
+ switch (mGravity) {
|
|
|
+ case 1:
|
|
|
+ layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
|
|
|
+ case 2:
|
|
|
+ layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ ((ViewHolder) holder).itemRelative.addView(imageView, layoutParams);
|
|
|
+ imageView.setOnClickListener(view -> remove(position));
|
|
|
+ }
|
|
|
+ GlideUtils.loadImg(((ViewHolder) holder).img, mData.get(position), mDefaultRes);
|
|
|
+ ((ViewHolder) holder).img.setOnClickListener(view -> {
|
|
|
+ StringBuilder urls = new StringBuilder();
|
|
|
+ for (int i = 0; i < mData.size(); i++) {
|
|
|
+ urls.append(mData.get(i)).append(",");
|
|
|
+ }
|
|
|
+ if (urls.length() > 0 && urls.length() > 1) {
|
|
|
+ urls.deleteCharAt(urls.length() - 1);
|
|
|
+ } else {
|
|
|
+ urls = new StringBuilder();
|
|
|
+ }
|
|
|
+ Intent intent = new Intent(holder.itemView.getContext(), ImageDetailsActivity.class);
|
|
|
+ Bundle bundle = new Bundle();
|
|
|
+ bundle.putString("urls", urls.toString());
|
|
|
+ bundle.putString("indexUrl", mData.get(position));
|
|
|
+ bundle.putInt("mDefaultRes", mDefaultRes);
|
|
|
+ holder.itemView.getContext().startActivity(intent, bundle);
|
|
|
+ });
|
|
|
+ } else if (holder instanceof FooterViewHolder) {
|
|
|
+ if (mAddRes > 0) {
|
|
|
+ ((FooterViewHolder) holder).img.setImageResource(mAddRes);
|
|
|
+ }
|
|
|
+ ((FooterViewHolder) holder).itemRelative.setOnClickListener(view -> {
|
|
|
+ if (onAddImgClick != null) {
|
|
|
+ onAddImgClick.onAddImgClick();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setOnAddImgClick(ImageRecycler.OnAddImgClick onAddImgClick) {
|
|
|
+ this.onAddImgClick = onAddImgClick;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getItemCount() {
|
|
|
+ int size = 0;
|
|
|
+ if (isAdd) {
|
|
|
+ if (mMinShowSize > 0) {
|
|
|
+ size = mMinShowSize;
|
|
|
+ } else {
|
|
|
+ size += 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (mMaxLength <= 0 || mData.size() <= mMaxLength) {
|
|
|
+ size += mData.size();
|
|
|
+ } else {
|
|
|
+ size += mMaxLength;
|
|
|
+ }
|
|
|
+ return size;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getItemViewType(int position) {
|
|
|
+ if (isFooter(position)) {
|
|
|
+ return FOOTER_TYPE;
|
|
|
+ } else {
|
|
|
+ return NONE_VIEW;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isFooter(int position) {
|
|
|
+ if (isAdd) {
|
|
|
+ if (mMinShowSize > 0) {
|
|
|
+ return position < getItemCount() && position >= getItemCount() - mMinShowSize;
|
|
|
+ } else {
|
|
|
+ return position < getItemCount() && position >= getItemCount() - 1;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Header、Footer挂靠到RecyclerView
|
|
|
+ *
|
|
|
+ * @param recyclerView
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
|
|
|
+ super.onAttachedToRecyclerView(recyclerView);
|
|
|
+ RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
|
|
|
+ if (manager instanceof GridLayoutManager) { // 布局是GridLayoutManager所管理
|
|
|
+ final GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
|
|
|
+ gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
|
|
|
+ @Override
|
|
|
+ public int getSpanSize(int position) {
|
|
|
+ // 如果是Header、Footer的对象则占据spanCount的位置,否则就只占用1个位置
|
|
|
+ return (isFooter(position)) ? gridLayoutManager.getSpanCount() : 1;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addData(@IntRange(from = 0L) int position, @NonNull String data) {
|
|
|
+ this.mData.add(position, data);
|
|
|
+ this.notifyItemInserted(position);
|
|
|
+ this.compatibilityDataSizeChanged(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addData(@NonNull String data) {
|
|
|
+ this.mData.add(data);
|
|
|
+ this.notifyItemInserted(this.mData.size());
|
|
|
+ this.compatibilityDataSizeChanged(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void remove(@IntRange(from = 0L) int position) {
|
|
|
+ this.mData.remove(position);
|
|
|
+ this.notifyItemRemoved(position);
|
|
|
+ this.compatibilityDataSizeChanged(0);
|
|
|
+ this.notifyItemRangeChanged(position, this.mData.size() - position);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setData(@IntRange(from = 0L) int index, @NonNull String data) {
|
|
|
+ this.mData.set(index, data);
|
|
|
+ this.notifyItemChanged(index);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addData(@IntRange(from = 0L) int position, @NonNull Collection<? extends String> newData) {
|
|
|
+ this.mData.addAll(position, newData);
|
|
|
+ this.notifyItemRangeInserted(position, newData.size());
|
|
|
+ this.compatibilityDataSizeChanged(newData.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addData(@NonNull Collection<? extends String> newData) {
|
|
|
+ this.mData.addAll(newData);
|
|
|
+ this.notifyItemRangeInserted(this.mData.size() - newData.size(), newData.size());
|
|
|
+ this.compatibilityDataSizeChanged(newData.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void replaceData(@NonNull Collection<? extends String> data) {
|
|
|
+ if (data != this.mData) {
|
|
|
+ this.mData.clear();
|
|
|
+ this.mData.addAll(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ this.notifyDataSetChanged();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void compatibilityDataSizeChanged(int size) {
|
|
|
+ int dataSize = this.mData == null ? 0 : this.mData.size();
|
|
|
+ if (dataSize == size) {
|
|
|
+ this.notifyDataSetChanged();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @NonNull
|
|
|
+ public List<String> getData() {
|
|
|
+ return this.mData;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Nullable
|
|
|
+ public String getItem(@IntRange(from = 0L) int position) {
|
|
|
+ return position >= 0 && position < this.mData.size() ? this.mData.get(position) : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ static class ViewHolder extends RecyclerView.ViewHolder {
|
|
|
+
|
|
|
+ private RelativeLayout itemRelative;
|
|
|
+ private ImageView img;
|
|
|
+
|
|
|
+ public ViewHolder(@NonNull View itemView) {
|
|
|
+ super(itemView);
|
|
|
+ init(itemView);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void init(View itemView) {
|
|
|
+ itemRelative = itemView.findViewById(R.id.item_item);
|
|
|
+ img = itemView.findViewById(R.id.item_img);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ static class FooterViewHolder extends RecyclerView.ViewHolder {
|
|
|
+
|
|
|
+ private RelativeLayout itemRelative;
|
|
|
+ private ImageView img;
|
|
|
+
|
|
|
+ public FooterViewHolder(@NonNull View itemView) {
|
|
|
+ super(itemView);
|
|
|
+ init(itemView);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void init(View itemView) {
|
|
|
+ itemRelative = itemView.findViewById(R.id.item_item);
|
|
|
+ img = itemView.findViewById(R.id.item_img);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|