java - Android: GridView shows all but a few images -
i have 6 images downloaded as shown here, gridview in gallery displays 5 of images.
i'm trying copy how instagram displays gallery, selected image taking 60% of screen , gallery images taking rest.
fragment_gallery.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <relativelayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/rellayoutl"> <!--toolbar--> <include layout="@layout/snippet_top_gallerybar"/> </relativelayout> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightsum="100" android:layout_below="@+id/rellayoutl"> <relativelayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="60"> <imageview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/galleryimageview" android:scaletype="centercrop"/> <progressbar android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/progressbar" android:layout_centerinparent="true"/> </relativelayout> <relativelayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="40"> <gridview android:layout_width="match_parent" android:layout_height="fill_parent" android:numcolumns="5" android:verticalspacing="1.5dp" android:horizontalspacing="1.5dp" android:gravity="center" android:layout_margintop="1dp" android:stretchmode="none" android:id="@+id/gridview"> </gridview> </relativelayout> </linearlayout> </relativelayout>
i created square view generate square cells
layout_grid_imageview.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"> <com.example.sheldon.instagramclone.util.squareimageview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/gridviewimage" android:adjustviewbounds="true" android:scaletype="centercrop"/> <progressbar android:layout_width="50dp" android:layout_height="50dp" android:layout_centerinparent="true" android:id="@+id/gridprogressbar"/> </relativelayout>
galleryfragment.java
public class galleryfragment extends fragment { private static final int num_columns = 4; private imageview mexit; private spinner mspinner; private textview mnext; private progressbar mprogressbar; private list<string> directories; private gridview mgridview; private imageview mgalleryimage; private hashmap<string, arraylist<string>> directorytoimage; private string append = "file:/"; private string mselectedimage; @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_gallery, container, false); mexit = (imageview) view.findviewbyid(r.id.exitshare); mspinner = (spinner) view.findviewbyid(r.id.sharespinner); mnext = (textview) view.findviewbyid(r.id.sharenext); mprogressbar = (progressbar) view.findviewbyid(r.id.progressbar); mgridview = (gridview) view.findviewbyid(r.id.gridview); mgalleryimage = (imageview) view.findviewbyid(r.id.galleryimageview); mexit.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { getactivity().finish(); } }); mnext.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { log.d(tag, "onclick: navigating next step in sharing photo"); intent intent = new intent(getactivity(), nextactivity.class); intent.putextra("selected_image", mselectedimage); startactivity(intent); } }); init(); return view; } private void init() { imagefinder imagefinder = new imagefinder(); imagefinder.getimages(getactivity()); directorytoimage = imagefinder.getimagemapping(); directories = new arraylist<>(directorytoimage.keyset()); arrayadapter<string> adapter = new arrayadapter<string>(getactivity(), r.layout.spinner_item, directories); adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); mspinner.setadapter(adapter); mspinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { log.d(tag, "onitemselected: " + directories.get(position)); setupgridview(directories.get(position)); } @override public void onnothingselected(adapterview<?> parent) { } }); } private void setupgridview(string directory) { final arraylist<string> imgurls = directorytoimage.get(directory); log.d(tag, "setupgridview: displaying " + directory + " " + imgurls.size() + " images"); int gridwidth = getresources().getdisplaymetrics().widthpixels; int imagewidth = gridwidth / num_columns; log.d(tag, "setupgridview: image width " + imagewidth); mgridview.setcolumnwidth(imagewidth); gridimageadapter adapter = new gridimageadapter(getactivity(), r.layout.layout_grid_imageview, append, imgurls); mgridview.setadapter(adapter); universalimageloader.setimage(imgurls.get(0),mgalleryimage, mprogressbar, append); mselectedimage = imgurls.get(0); mgridview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { universalimageloader.setimage(imgurls.get(position), mgalleryimage, mprogressbar, append); mselectedimage = imgurls.get(0); } });}
i display images using library called universal image loader
gridimageadapter.java
public class gridimageadapter extends arrayadapter<string>{ private context mcontext; private layoutinflater minflater; private int layoutresource; private string mappend; private arraylist<string> imgurls; public gridimageadapter(context context, int layoutresource, string append, arraylist<string> imgurls) { super(context, layoutresource, imgurls); minflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); mcontext = context; this.layoutresource = layoutresource; mappend = append; this.imgurls = imgurls; } private static class viewholder{ squareimageview image; progressbar mprogressbar; } @nonnull @override public view getview(int position, @nullable view convertview, @nonnull viewgroup parent) { final viewholder holder; if(convertview == null){ convertview = minflater.inflate(layoutresource, parent, false); holder = new viewholder(); holder.mprogressbar = (progressbar) convertview.findviewbyid(r.id.gridprogressbar); holder.image = (squareimageview) convertview.findviewbyid(r.id.gridviewimage); convertview.settag(holder); } else{ holder = (viewholder) convertview.gettag(); } string imgurl = getitem(position); log.d(tag, "getview: loading position " + position + ", displaying " + imgurl + ", image " + holder.image); imageloader imageloader = imageloader.getinstance(); imageloader.displayimage(mappend + imgurl, holder.image, new imageloadinglistener() { @override public void onloadingstarted(string imageuri, view view) { if(holder.mprogressbar != null){ holder.mprogressbar.setvisibility(view.visible); } } @override public void onloadingfailed(string imageuri, view view, failreason failreason) { if(holder.mprogressbar != null){ holder.mprogressbar.setvisibility(view.gone); } } @override public void onloadingcomplete(string imageuri, view view, bitmap loadedimage) { if(holder.mprogressbar != null){ holder.mprogressbar.setvisibility(view.gone); } } @override public void onloadingcancelled(string imageuri, view view) { if(holder.mprogressbar != null){ holder.mprogressbar.setvisibility(view.gone); } } }); return convertview; }
first time posting, apologize if there's wrong post.
sorry being general , unclear in post, wasn't quite sure portion of code problem area. after looking on code again, noticed stupid mistake. set gridview's numcolumns attribute 5 in fragment_gallery.xml, calculated column width in galleryfragment.java using private static final int num_columns = 4. assume caused images displayed in non-existent 5th column.
Comments
Post a Comment