3.2 send_item.xml,上传相册,代码如下:
 <?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_margin=“4dp”
android:orientation=“vertical”>
<ImageView
android:id=“@+id/imageView6”
android:layout_width=“100dp”
android:layout_height=“100dp”
android:adjustViewBounds=“true”
android:scaleType=“centerCrop”
android:layout_gravity=“center”/>
</android.support.v7.widget.CardView>
4.RecyclerViewAddImageAdapter.java中,代码如下:
private static final int ITEM_ONE = 1;//1表示相册
private static final int ITEM_TWO = 2;//2表示+号
private Context context;
private List data;
private Bitmap bitmap;
private OnItemClickListener clickListener;//点击事件(相册和+号)
private OnItemLongClickListener longclickListener;//长按对应刚上传的相册就要删除
private int MAXIMAGES = 10;//10表示九张相册和一个+号
public RecyclerViewAddImageAdapter(Context context, List data) {
this.context = context;
this.data = data;
}
public void setClickListener(OnItemClickListener clickListener) {
this.clickListener = clickListener;
}
public void setLongClickListener(OnItemLongClickListener longclickListener) {
this.longclickListener = longclickListener;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
RecyclerView.ViewHolder holder;
if (viewType == ITEM_TWO) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.send_item_add, parent, false);
holder = new AnoViewHolder(view);
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.send_item, parent, false);
holder = new ViewHolder(view);
}
return holder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof AnoViewHolder) {
} else {
bitmap = FileUtil.bitmapFactory(context, data.get(position));
((ViewHolder) holder).imageView.setImageBitmap(bitmap);
}
}
@Override
public int getItemCount() {
//data如果是null,就为1,1表示+号,不是1,表示刚上传相册数量
int count = data == null ? 1 : data.size() + 1;
if (count >= MAXIMAGES) {
return data.size();//上传的相册数量,暂时不加+号
} else {
return count;//上传相册数量,并且加+号
}
}
@Override
public int getItemViewType(int position) {
if (position > data.size() - 1) {
return ITEM_TWO;
} else {
return ITEM_ONE;
}
}
public interface OnItemClickListener {
void onClick(View view, int position);//点击事件(刚上传的数量相册,+号)
void onSelected(View view, int position);//点击事件(刚上传的数量相册)
}
public interface OnItemLongClickListener {
void onLongClick(View view, int position);
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView6);
imageView.setOnClickListener(this);
imageView.setOnLongClickListener(this);
}
@Override
public void onClick(View v) {
if (clickListener != null) {
clickListener.onSelected(itemView, getAdapterPosition());
}
}
@Override
public boolean onLongClick(View v) {
if (longclickListener != null) {
longclickListener.onLongClick(imageView, getAdapterPosition());
}
return false;
}
}
public class AnoViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView img;
public AnoViewHolder(View itemView) {
super(itemView);
img = itemView.findViewById(R.id.defaultimg);
img.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (clickListener != null) {
clickListener.onClick(itemView, getAdapterPosition());
}
}
}