1.添加依赖
implementation 'com.github.bumptech.glide:glide:4.12.0' //裁剪图片等等 implementation 'androidx.recyclerview:recyclerview:1.1.0' //recycleview依赖
2.使用recycleview
3.编写两个item页面为recycleview的子页面
4.写recycleview的适配器
package com.example.myapplication; import android.content.context; import android.content.intent; import android.net.uri; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.imageview; import androidx.annotation.nonnull; import androidx.recyclerview.widget.recyclerview; import com.bumptech.glide.glide; import java.util.arraylist; public class imageadapter extends recyclerview.adapter{ private final int view_type_image = 0; private final int view_type_add_button = 1; private arraylist imageuris; private context context; private layoutinflater inflater; private onaddbuttonclicklistener addbuttonclicklistener; public interface onaddbuttonclicklistener { void onaddbuttonclick(); } public imageadapter(context context, onaddbuttonclicklistener listener) { this.context = context; this.imageuris = new arraylist<>(); this.inflater = layoutinflater.from(context); this.addbuttonclicklistener = listener; } public recyclerview.viewholder oncreateviewholder(@nonnull viewgroup parent, int viewtype) { if (viewtype == view_type_image) { view itemview = inflater.inflate(r.layout.iteam, parent, false); return new imageviewholder(itemview); } else { view itemview = inflater.inflate(r.layout.iteambutton, parent, false); button addbutton = itemview.findviewbyid(r.id.btnadd); addbutton.setonclicklistener(v -> addbuttonclicklistener.onaddbuttonclick()); return new addbuttonviewholder(itemview); } } @override public void onbindviewholder(@nonnull recyclerview.viewholder holder, int position) { if (getitemviewtype(position) == view_type_image) { uri uri = imageuris.get(position); imageviewholder imageholder = (imageviewholder) holder; glide.with(context) .load(uri) .centercrop() .into( imageholder.imageview); //点击图片 放大图片,跳转页面 imageholder.imageview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent=new intent(context,emptyactivity.class); intent.putextra("url",uri.tostring()); context.startactivity(intent); } }); } } @override public int getitemcount() { // plus one for the add button return imageuris.size() 1 } @override public int getitemviewtype(int position) { return position == imageuris.size() ? view_type_add_button : view_type_image; } public void addimage(uri uri) { imageuris.add(uri); notifyiteminserted(imageuris.size() - 1); } static class imageviewholder extends recyclerview.viewholder { imageview imageview; imageviewholder(view itemview) { super(itemview); imageview = itemview.findviewbyid(r.id.imgview); } } static class addbuttonviewholder extends recyclerview.viewholder { addbuttonviewholder(view itemview) { super(itemview); } } }
5.编写java文件
package com.example.myapplication; import android.content.intent; import android.net.uri; import android.os.bundle; import android.provider.mediastore; import androidx.annotation.nullable; import androidx.appcompat.app.appcompatactivity; import androidx.recyclerview.widget.gridlayoutmanager; import androidx.recyclerview.widget.linearlayoutmanager; import androidx.recyclerview.widget.recyclerview; import java.util.arraylist; public class recycleactivity extends appcompatactivity implements imageadapter.onaddbuttonclicklistener{ private static final int request_code_pick_image = 1; private imageadapter imageadapter; private recyclerview recyclerview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_recy_cle); recyclerview = findviewbyid(r.id.recyclerview); recyclerview.setlayoutmanager(new gridlayoutmanager(this,3)); imageadapter = new imageadapter(this, this); recyclerview.setadapter(imageadapter); } @override public void onaddbuttonclick() { intent intent = new intent(intent.action_pick, mediastore.images.media.external_content_uri); startactivityforresult(intent, request_code_pick_image); } protected void onactivityresult(int requestcode, int resultcode, @nullable intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == request_code_pick_image && resultcode == result_ok) { if (data != null) { uri selectedimageuri = data.getdata(); if (selectedimageuri != null) { imageadapter.addimage(selectedimageuri); } } } } }
6.编写点击图片后放大图片,创建emptyactivity页面
package com.example.myapplication; import androidx.appcompat.app.appcompatactivity; import android.net.uri; import android.os.bundle; import android.widget.imageview; import com.bumptech.glide.glide; import javax.microedition.khronos.opengles.gl; public class emptyactivity extends appcompatactivity { private imageview img; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_empty); img=findviewbyid(r.id.img); string imageuristring = getintent().getstringextra("url"); glide.with(emptyactivity.this) .load(imageuristring) .into(img); } }