1.下载个加号
2.
maven { url "https://jitpack.io" }
//支持多选的安卓相册 compile 'com.yanzhenjie:album:2.0.2' //Glide compile 'com.github.bumptech.glide:glide:3.7.0' //RecyclerView 快速适配器 compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30' //design26.1.0 implementation 'com.android.support:design:26.1.0'
3.Adapter
public class MyAdapter extends BaseQuickAdapter<ImageInfo, BaseViewHolder> {
public MyAdapter(int layoutResId, @Nullable List<ImageInfo> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, ImageInfo item) {
Object imagePathOrResId = item.getImagePathOrResId();
ImageView imageItem = helper.getView(R.id.imageItem);
if (imagePathOrResId instanceof String) {
File file = CompressHelper.getDefault(mContext).compressToFile(new File((String) imagePathOrResId));
Glide.with(mContext)
.load(file)
.into(imageItem);
} else {
int imagePathOrResId1 = (int) imagePathOrResId;
Glide.with(mContext)
.load(imagePathOrResId1)
.into(imageItem);
}
}
// @Override
// public int getItemCount() {
// return 6;
// }
}
4.Bean
public class ImageInfo {
private Object imagePathOrResId;
public Object getImagePathOrResId() {
return imagePathOrResId;
}
public void setImagePathOrResId(Object imagePathOrResId) {
this.imagePathOrResId = imagePathOrResId;
}
public ImageInfo(Object imagePathOrResId) {
this.imagePathOrResId = imagePathOrResId;
}
@Override
public String toString() {
return "ImageInfo{" +
"imagePathOrResId=" + imagePathOrResId +
'}';
}
}
5.MainAc
public class MainActivity extends AppCompatActivity {
private int ACTIVITY_REQUEST_SELECT_PHOTO = 12138;//随意
private RecyclerView recyclerView;
private int size;
private List<ImageInfo> list = new ArrayList<>();
private MyAdapter myAdapter;
private int count = 6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
list.add(new ImageInfo(R.drawable.add));
myAdapter = new MyAdapter(R.layout.item_image, list);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(myAdapter);
myAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
if (list.get(position).getImagePathOrResId() instanceof String || list.size() >= 7) {
return;
}
Album.image(MainActivity.this) // 选择图片。
.multipleChoice()
.requestCode(ACTIVITY_REQUEST_SELECT_PHOTO)
.camera(true)//有照相机
.columnCount(3)//一行三个
.selectCount(count)//选择一个
.onResult(new Action<ArrayList<AlbumFile>>() {
@Override
public void onAction(int requestCode, @NonNull ArrayList<AlbumFile> result) {
size = result.size();//选了几个
count = count - size;
if (size > 0) {
for (int i = 0; i < size; i++) {
list.add(new ImageInfo(result.get(i).getPath()));
}
for (int i = 0; i < list.size(); i++) {
Object imagePathOrResId = list.get(i).getImagePathOrResId();
if (imagePathOrResId instanceof Integer) {
list.remove(i);
}
}
if (list.size() < 6) {
list.add(new ImageInfo(R.drawable.add));
}
myAdapter.notifyDataSetChanged();
}
}
})
.start();
}
});
myAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
if(list.size()>0){
if(list.get(position).getImagePathOrResId() instanceof String){
myAdapter.remove(position);
count++;
}
if(list.size()==5 && list.get(4).getImagePathOrResId() instanceof String){
list.add(new ImageInfo(R.drawable.add));
}
}
return true;
}
});
}
private void initView() {
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
}
}
6.MainXml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="horizontal"
tools:context="com.as.mydemo666.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
7.item_image
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageItem"
android:layout_width="100dp"
android:layout_height="100dp"
android:padding="5dp"
android:scaleType="fitXY" />
</LinearLayout>