parent
23d701417d
commit
b86e2e3d1d
@ -0,0 +1,3 @@
|
|||||||
|
package com.common.commonlib.bean
|
||||||
|
|
||||||
|
data class GridPicBean(val imagePath:String, val isConverting: Boolean)
|
@ -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<RecyclerView.ViewHolder> {
|
||||||
|
private static final int TYPE_ONE = 0;
|
||||||
|
private static final int TYPE_TWO = 1;
|
||||||
|
private final List<GridPicBean> 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<String> getImagePathList() {
|
||||||
|
List<String> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="@dimen/font_80dp"
|
||||||
|
android:layout_height="@dimen/font_80dp">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/bg_pic_add"
|
||||||
|
android:layout_width="@dimen/font_74dp"
|
||||||
|
android:layout_height="@dimen/font_74dp"
|
||||||
|
android:layout_alignParentStart="true"
|
||||||
|
android:layout_alignParentBottom="true"
|
||||||
|
android:src="@drawable/ic_add" />
|
||||||
|
</RelativeLayout>
|
||||||
|
|
@ -0,0 +1,31 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="@dimen/font_80dp"
|
||||||
|
android:layout_height="@dimen/font_80dp">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/pic_iv"
|
||||||
|
android:layout_width="@dimen/font_74dp"
|
||||||
|
android:layout_height="@dimen/font_74dp"
|
||||||
|
android:layout_alignParentStart="true"
|
||||||
|
android:layout_alignParentBottom="true"
|
||||||
|
android:scaleType="center" />
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/pic_delete_iv"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_alignTop="@+id/pic_iv"
|
||||||
|
android:layout_alignEnd="@+id/pic_iv"
|
||||||
|
android:layout_marginTop="-5dp"
|
||||||
|
android:layout_marginEnd="-5dp"
|
||||||
|
android:src="@drawable/ic_pic_delete" />
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/iv_loading"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:background="#33000000"
|
||||||
|
android:padding="@dimen/font_20dp"
|
||||||
|
android:visibility="gone" />
|
||||||
|
</RelativeLayout>
|
Loading…
Reference in New Issue