android - ImageView size issue within ListView -
i encounter error when first load listview images url size not displayed correctly. happens first time.
if go previous fragment , again listview, image sizes fixed ( although can see momentarily had wrong size before being fixed ).
i use android query retrieve images , example listview : http://javatechig.com/android/android-listview-tutorial
searchresultadapter.java
public class searchresultadapter extends baseadapter{ private context context; private arraylist listdata; private layoutinflater layoutinflater; public searchresultadapter(context context, arraylist listdata) { this.context = context; this.listdata = listdata; layoutinflater = layoutinflater.from(context); } @override public int getcount() { return listdata.size(); } @override public object getitem(int position) { return listdata.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = layoutinflater.inflate(r.layout.searchresult_single_item, null); holder = new viewholder(); holder.adimage = (imageview) convertview.findviewbyid(r.id.adimage); holder.adtitle = (textview) convertview.findviewbyid(r.id.adtitle); holder.adprice = (textview) convertview.findviewbyid(r.id.adprice); holder.adsize = (textview) convertview.findviewbyid(r.id.adsize); holder.addetails = (textview) convertview.findviewbyid(r.id.addetails); holder.admessage = (imageview) convertview.findviewbyid(r.id.admessage); holder.adfavorite = (imageview) convertview.findviewbyid(r.id.adfavorite); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } aditem adlistitem = (aditem) listdata.get(position); aquery aq = new aquery(convertview); aq.id(holder.adimage).image(adlistitem.getadimageurl(), true, true, 50, 0, null, aquery.fade_in_network, 1.0f); imageloader imgloader = new imageloader(context); imgloader.displayimage(adlistitem.getadimageurl(), holder.adimage); holder.adtitle.settext(adlistitem.getadtitle()); holder.adprice.settext(adlistitem.getadprice()); holder.adsize.settext(adlistitem.getadsize()); holder.addetails.settext(adlistitem.getaddetails()); return convertview; } static class viewholder { imageview adimage; textview adtitle; textview adprice; textview adsize; textview addetails; imageview admessage; imageview adfavorite; } }
searchresultfragment.java
public class searchresultfragment extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view view = inflater.inflate(r.layout.fragment_searchresult, container, false); arraylist listdata = getlistdata(); final listview searchresult = (listview) view.findviewbyid(r.id.listviewsearchresult); searchresult.setadapter(new searchresultadapter(getactivity(), listdata)); searchresult.invalidateviews(); return view; } private arraylist getlistdata() { arraylist results = new arraylist(); aditem addata = new aditem(); addata.setadimageurl("https://img.wg-gesucht.de/media/up/2014/36/3d3eb5b646560480_2014_09_07_19_26_33.sized.jpg"); addata.setadtitle("apartment"); addata.setadprice("€300"); addata.setadsize("37m"); addata.setaddetails("this wonderful apartment @ reasonable price."); results.add(addata); addata = new aditem(); addata.setadimageurl("https://img.wg-gesucht.de/media/up/2014/46/a37ed74847504440_img_7409.sized.jpg"); addata.setadtitle("apartment"); addata.setadprice("€450"); addata.setadsize("50m"); addata.setaddetails("this wonderful apartment @ reasonable price."); results.add(addata); addata = new aditem(); addata.setadimageurl("https://img.wg-gesucht.de/media/up/2014/46/4399903447385080_img_3106.sized.jpg"); addata.setadtitle("apartment"); addata.setadprice("€500"); addata.setadsize("62m"); addata.setaddetails("this wonderful apartment @ reasonable price."); results.add(addata); return results; } }
this fragment layout row item :
<imageview android:id="@+id/adimage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_centervertical="true" android:minwidth="50dp" android:minheight="50dp" android:layout_marginright="20dp"/> <textview android:id="@+id/adtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:layout_alignparenttop="true" android:layout_torightof="@id/adimage" /> <textview android:id="@+id/adprice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:layout_below="@id/adtitle" android:layout_torightof="@id/adimage" /> <textview android:id="@+id/adsize" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:layout_below="@id/adtitle" android:layout_marginleft="10dp" android:layout_torightof="@id/adprice" /> <textview android:id="@+id/addetails" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:layout_torightof="@id/adimage" android:layout_below="@id/adprice"/> <imageview android:id="@+id/adfavorite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_alignparentbottom="true"/> <imageview android:id="@+id/admessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toleftof="@id/adfavorite"/>
first image looks first time listview loaded , looks after ( 2nd image should ).
if not necessary keep min width/height, can 2nd image result changing imageview attributes in list row to:
<imageview android:id="@+id/adimage" android:layout_width="70dp" android:layout_height="50dp" android:layout_alignparentleft="true" android:layout_centervertical="true" android:layout_marginright="20dp" android:scaletype="centercrop"/>
Comments
Post a Comment