android - StaggeredGridView stucks inbetween while scrolling -
i displaying images storage of device in app.the problem having when scrolling not smooth.its stucking inbetween.i using etsy/androidstaggeredgrid , using below code.i tried putting tasks inside async background task still having issue. why stucking inbetween , how can resolve this?
galleryfragment
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { final view rootview = inflater.inflate(r.layout.fragment_gallery, container, false); galleries=new arraylist<gallery>(); ((actionbaractivity) getactivity()).getsupportactionbar().show(); gridview = (staggeredgridview)rootview.findviewbyid(r.id.gridcamera); progressbar=(progressbar)rootview.findviewbyid(r.id.progressbar); gallerylist=new cameragalleryadapter(getactivity().getapplicationcontext(), r.layout.gallery_grid,galleries); gridview.setadapter(gallerylist); asynctaskrunner runner = new asynctaskrunner(); runner.execute(); return rootview; } private class asynctaskrunner extends asynctask<string, string, void> { private string resp; @override protected void doinbackground(string... params) { int count,count2,count3,count4; ((cameraactivity)getactivity()).setonbackpressedlistener(new basebackpressedlistener(getactivity(),true)); final string[] columns = { mediastore.images.media.data, mediastore.images.media._id,mediastore.video.media.data, }; final string orderby = mediastore.images.media._id; cursor cursor = getactivity().getcontentresolver().query( mediastore.images.media.external_content_uri, columns, null, null, orderby); count = cursor.getcount(); cursor cursor2 = getactivity().getcontentresolver().query( mediastore.images.media.internal_content_uri, columns, null, null, orderby); count2 = cursor2.getcount(); cursor cursor3 = getactivity().getcontentresolver().query( mediastore.video.media.external_content_uri, columns, null, null, orderby); count3 = cursor3.getcount(); cursor cursor4 = getactivity().getcontentresolver().query( mediastore.video.media.internal_content_uri, columns, null, null, orderby); count4 = cursor4.getcount(); (int = 0; < count; i++) { cursor.movetoposition(i); int datacolumnindex = cursor.getcolumnindex(mediastore.images.media.data); gallery ga=new gallery(); ga.setimageurl(cursor.getstring(datacolumnindex)); ga.setisimage(true); galleries.add(ga); } (int = 0; < count2; i++) { cursor2.movetoposition(i); int datacolumnindex = cursor2.getcolumnindex(mediastore.images.media.data); gallery ga=new gallery();; ga.setimageurl(cursor2.getstring(datacolumnindex)); ga.setisimage(true); galleries.add(ga); } (int = 0; < count3; i++) { cursor3.movetoposition(i); int datacolumnindex = cursor3.getcolumnindex(mediastore.video.media.data); bitmap thumb = thumbnailutils.createvideothumbnail(cursor3.getstring(datacolumnindex), mediastore.images.thumbnails.mini_kind); gallery ga=new gallery(); ga.setvideothumbnail(thumb); ga.setvideourl(cursor3.getstring(datacolumnindex)); ga.setisimage(false); galleries.add(ga); } (int = 0; < count4; i++) { cursor4.movetoposition(i); int datacolumnindex = cursor4.getcolumnindex(mediastore.video.media.data); bitmap thumb = thumbnailutils.createvideothumbnail(cursor4.getstring(datacolumnindex),mediastore.images.thumbnails.mini_kind); gallery ga = new gallery(); ga.setvideothumbnail(thumb); ga.setvideourl(cursor4.getstring(datacolumnindex)); ga.setisimage(false); galleries.add(ga); } return null; } @override protected void onpreexecute() { } @override protected void onpostexecute(void avoid) { super.onpostexecute(avoid); gallerylist.notifydatasetchanged(); progressbar.setvisibility(view.gone); } @override protected void onprogressupdate(string... text) { } }
cameragalleryadapter
public class cameragalleryadapter extends baseadapter { arraylist<gallery> itemlist; layoutinflater vi; int resource; viewholder holder; bitmap bm; public cameragalleryadapter(context context, int resource, arraylist<gallery> objects) { vi = (layoutinflater) context .getsystemservice(context.layout_inflater_service); resource = resource; itemlist = objects; } @override public int getcount() { return itemlist.size(); } @override public object getitem(int i) { return null; } @override public long getitemid(int i) { return 0; } @override public view getview(int position, view convertview, viewgroup parent) { view v = convertview; if (v == null) { holder = new viewholder(); v = vi.inflate(resource, null); holder.imageview = (imageview) v.findviewbyid(r.id.ivimageg); holder.playicon= (imageview) v.findviewbyid(r.id.galleryplay); v.settag(holder); } else { holder = (viewholder) v.gettag(); } if(itemlist.get(position).getisimage()) { holder.playicon.setvisibility(view.invisible); asynctaskrunner runner = new asynctaskrunner(); runner.execute(itemlist.get(position).getimageurl()); bm= null ; try { bm = runner.get(); } catch (interruptedexception e) { e.printstacktrace(); } catch (executionexception e) { e.printstacktrace(); } holder.imageview.setimagebitmap(bm); } else { holder.playicon.setvisibility(view.visible); holder.imageview.setimagebitmap(itemlist.get(position).getvideothumbnail()); } return v; } static class viewholder { public imageview imageview; public imageview playicon; } private class asynctaskrunner extends asynctask<string, string,bitmap> { private string resp; @override protected bitmap doinbackground(string... params) { resp=params[0]; bitmap bm = decodesampledbitmapfromuri(resp, 220, 220); return bm; } @override protected void onpreexecute() { } @override protected void onpostexecute(bitmap avoid) { super.onpostexecute(avoid); } @override protected void onprogressupdate(string... text) { } } public bitmap decodesampledbitmapfromuri(string path, int reqwidth, int reqheight) { bitmap bm = null; final bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodefile(path, options); options.insamplesize = calculateinsamplesize(options, reqwidth, reqheight); options.injustdecodebounds = false; bm = bitmapfactory.decodefile(path, options); return bm; } public int calculateinsamplesize( bitmapfactory.options options, int reqwidth, int reqheight) { final int height = options.outheight; final int width = options.outwidth; int insamplesize = 1; if (height > reqheight || width > reqwidth) { if (width > height) { insamplesize = math.round((float)height / (float)reqheight); } else { insamplesize = math.round((float)width / (float)reqwidth); } } return insamplesize; } }
its common issue bcoz of large image size loading in view each time scroll .... on come issue can try lazy loading imgae technique.. http://androidexample.com/download_images_from_web_and_lazy_load_in_listview_-_android_example/index.php?view=article_discription&aid=112&aaid=134
Comments
Post a Comment