.placeholder(R.drawable.ic_launcher_background)
.into(imageView);
}
  • 2、创建列表布局文件  activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“.MainActivity”>
<GridView
android:id=“@+id/gvPic”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:horizontalSpacing=“10dp”
android:layout_marginLeft=“10dp”
android:layout_marginRight=“10dp”
android:layout_marginTop=“80dp”
android:numColumns=“4”
tools:ignore=“MissingPrefix” />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
  • 3、创建列表每一项布局文件gv_camera_pic_item.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:orientation=“vertical”>
<ImageView
android:id=“@+id/ivCameraPicDefault”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:background=“@drawable/iv_camera_round_bg”
android:scaleType=“center”
android:src=“@drawable/ic_camera” />
<ImageView
android:id=“@+id/ivCameraPic”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:visibility=“gone” />
<ImageView
android:id=“@+id/ivDelete”
android:layout_width=“40dp”
android:layout_height=“40dp”
android:layout_alignParentTop=“true”
android:layout_alignParentRight=“true”
android:background=“@drawable/ic_delete”
android:visibility=“gone” />
  • 4、创建列表适配器:

适配器配器构造函数:启动Activity完成初始化就将picDefaultId相机图片添加到数组队列中,添加点击事件是方便点击相机图标进行选择手相册图片或拍照:

ImageAdapter(Context c, int picDefaultId, GvItemClick gvItemClick),

点击默认相机图标就会在图片数组中添加成功选择或者拍照的图片;

点击新增图片右上角的删除图标,对当前在该数组存在的图片进行逐个删除.

changeAdapterAddData(Integer picId)和changeAdapteRemoverData(int i)

计算每张图片需要的宽与高,运用这个算法获取的宽度,然后动态为ImageView添加布局参数。

public int getGvItemWidth(Context context) {
Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
int width = display.getWidth();
//int height = display.getHeight();
return (width - dip2px(context, 50)) / 4;
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private List mPics = new ArrayList<>();
private GvItemClick mGvItemClick;
public ImageAdapter(Context c, int picDefaultId, GvItemClick gvItemClick) {
mContext = c;
mPics.add(picDefaultId);
this.mGvItemClick = gvItemClick;
}
/**
  • 刷新列表数据

  • @param picId
*/
public void changeAdapterAddData(Integer picId) {
mPics.add(0, picId);
//mPics.addAll(0, picIds);
notifyDataSetChanged();
}
public void changeAdapteRemoverData(int i) {
mPics.remove(i);
//mPics.addAll(0, picIds);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mPics.size();
}
@Override
public Object getItem(int position) {
return mPics.size() > 0 ? mPics.get(position) : null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.gv_camera_pic_item, null);
viewHolder = new ViewHolder();
viewHolder.ivCameraPic = convertView.findViewById(R.id.ivCameraPic);
viewHolder.ivCameraPicDefault = convertView.findViewById(R.id.ivCameraPicDefault);
viewHolder.ivDelete = convertView.findViewById(R.id.ivDelete);
//设置每一项宽和高
int width = getGvItemWidth(mContext);
viewHolder.ivCameraPic.setLayoutParams(new RelativeLayout.LayoutParams(width, width));
viewHolder.ivCameraPicDefault.setLayoutParams(new RelativeLayout.LayoutParams(width, width));
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
//End item
if (position == getCount() - 1) {
viewHolder.ivDelete.setVisibility(View.GONE);
viewHolder.ivCameraPicDefault.setVisibility(View.VISIBLE);
viewHolder.ivCameraPic.setVisibility(View.GONE);
viewHolder.ivCameraPicDefault.setImageResource(mPics.get(position));
} else {
viewHolder.ivDelete.setVisibility(View.VISIBLE);
viewHolder.ivCameraPicDefault.setVisibility(View.GONE);
viewHolder.ivCameraPic.setVisibility(View.VISIBLE);
viewHolder.ivCameraPic.setImageResource(mPics.get(position));
}
viewHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mGvItemClick != null)
mGvItemClick.gvItemClick(R.id.ivDelete, position);
}
});
viewHolder.ivCameraPicDefault.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mGvItemClick != null)
mGvItemClick.gvItemClick(R.id.ivCameraPicDefault, position);
}
});
return convertView;
}
public static interface GvItemClick {
void gvItemClick(int viewId, int postion);
}
/**
  • 获取gv每一项宽度

  • @param context
  • @return
*/
public int getGvItemWidth(Context context) {
Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
int width = display.getWidth();
//int height = display.getHeight();
return (width - dip2px(context, 50)) / 4;
}
public class ViewHolder {
ImageView ivDelete;
ImageView ivCameraPic;
ImageView ivCameraPicDefault;
}
/**
  • dp转px

  • @param context
  • @param dpValue
  • @return
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
  • px转dp

  • @param context
  • @param pxValue
  • @return
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}
  • 5、为GridView列表添加适配器:
private GridView mGvPic;
private ImageAdapter mImageAdapter;