Android gridview : Trying to reproduce interface + lags -
i have 2 problems :
- i trying reproduce interface (see below)
- my gridview lag much
i reproduce interface : each item has random height of imageview.
today here result :
what best way create gridview ?
here code of adapter :
import java.util.arraylist; import java.util.list; import java.util.random; import android.app.activity; import android.graphics.typeface; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.textview; import com.nostra13.universalimageloader.core.displayimageoptions; import com.nostra13.universalimageloader.core.imageloader; import com.nostra13.universalimageloader.core.imageloaderconfiguration; public class allannonces_custom_adapter extends arrayadapter<annonce> { typeface robotolight = typeface.createfromasset(getcontext().getassets(), "fonts/roboto-light.ttf"); typeface robotobold = typeface.createfromasset(getcontext().getassets(), "fonts/roboto-bold.ttf"); typeface robotoregular = typeface.createfromasset(getcontext().getassets(), "fonts/roboto-regular.ttf"); arraylist<integer> lista = new arraylist<integer>(); public allannonces_custom_adapter(activity activity, list<annonce> annonces) { super(activity, 0, annonces); lista.add(120); lista.add(170); lista.add(220); } @override public view getview(int position, view convertview, viewgroup parent) { activity activity = (activity) getcontext(); layoutinflater inflater = activity.getlayoutinflater(); view rowview; annonce annonce = getitem(position); //rowview = inflater.inflate(r.layout.all_annonces_item, null); rowview = inflater.inflate(r.layout.grid_single, null); //////////////////////////////////////////////////////// ///// image /////////////////////////////////////////////////////// //imageview imageannonce = (imageview) rowview.findviewbyid(r.id.allannonces_image) ; imageview imageannonce = (imageview) rowview.findviewbyid(r.id.grid_image) ; random r = new random(); imageannonce.getlayoutparams().height = lista.get(r.nextint(lista.size())); displayimageoptions options = new displayimageoptions.builder() .cacheinmemory(true) .cacheondisc(true) .build(); imageloaderconfiguration config = new imageloaderconfiguration.builder(getcontext()) .defaultdisplayimageoptions(options) .threadpriority(thread.max_priority) .denycacheimagemultiplesizesinmemory() .memorycachesize(2 * 1024 * 1024) .build(); imageloader imageloader = imageloader.getinstance(); imageloader.init(config); imageloader.displayimage(annonce.getimage(), imageannonce); //////////////////////////////////////////////////////// ///// title /////////////////////////////////////////////////////// //textview titre = (textview) rowview.findviewbyid(r.id.allannonces_titre); textview titre = (textview) rowview.findviewbyid(r.id.grid_text); titre.settext(annonce.gettitle()); titre.settypeface(robotoregular); //////////////////////////////////////////////////////// ///// ville objet /////////////////////////////////////////////////////// //textview villeobj = (textview) rowview.findviewbyid(r.id.allannonces_ville); textview villeobj = (textview) rowview.findviewbyid(r.id.grid_ville); villeobj.settext(annonce.getvilleobjet()); villeobj.settypeface(robotoregular); //////////////////////////////////////////////////////// ///// prix /////////////////////////////////////////////////////// //textview prix = (textview) rowview.findviewbyid(r.id.allannonces_prix); textview prix = (textview) rowview.findviewbyid(r.id.grid_prix); prix.settext("€" + annonce.getprix() + ",00 eur"); prix.settypeface(robotoregular); return rowview; } }
[update]
new code of adapter after remarks :
import android.app.activity; import android.graphics.typeface; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.textview; import com.nostra13.universalimageloader.core.displayimageoptions; import com.nostra13.universalimageloader.core.imageloader; import com.nostra13.universalimageloader.core.imageloaderconfiguration; public class allannonces_custom_adapter extends arrayadapter<annonce> { typeface robotolight = typeface.createfromasset(getcontext().getassets(), "fonts/roboto-light.ttf"); typeface robotobold = typeface.createfromasset(getcontext().getassets(), "fonts/roboto-bold.ttf"); typeface robotoregular = typeface.createfromasset(getcontext().getassets(), "fonts/roboto-regular.ttf"); public allannonces_custom_adapter(activity activity, list<annonce> annonces) { super(activity, 0, annonces); } @override public view getview(int position, view convertview, viewgroup parent) { viewholderitem viewholder; if (convertview==null) { activity activity = (activity) getcontext(); // inflate layout layoutinflater inflater = activity.getlayoutinflater(); convertview = inflater.inflate(r.layout.grid_single, parent, false); // set viewholder viewholder = new viewholderitem(); viewholder.imageannonce = (imageview) convertview.findviewbyid(r.id.grid_image) ; viewholder.titre = (textview) convertview.findviewbyid(r.id.grid_text); viewholder.villeobj = (textview) convertview.findviewbyid(r.id.grid_ville); viewholder.prix = (textview) convertview.findviewbyid(r.id.grid_prix); // store holder view. convertview.settag(viewholder); } else { viewholder = (viewholderitem) convertview.gettag(); } annonce annonce = getitem(position); if (annonce != null) { displayimageoptions options = new displayimageoptions.builder() .cacheinmemory(true) .cacheondisc(true) .build(); imageloaderconfiguration config = new imageloaderconfiguration.builder(getcontext()) .defaultdisplayimageoptions(options) .threadpriority(thread.max_priority) .denycacheimagemultiplesizesinmemory() .memorycachesize(2 * 1024 * 1024) .build(); imageloader imageloader = imageloader.getinstance(); imageloader.init(config); imageloader.displayimage(annonce.getimage(), viewholder.imageannonce); viewholder.titre.settext(annonce.gettitle()); viewholder.titre.settext(annonce.gettitle()); viewholder.titre.settypeface(robotoregular); viewholder.villeobj.settext(annonce.getvilleobjet()); viewholder.villeobj.settext(annonce.getvilleobjet()); viewholder.villeobj.settypeface(robotoregular); viewholder.prix.settext(annonce.getprix()); viewholder.prix.settext("€" + annonce.getprix() + ",00 eur"); viewholder.prix.settypeface(robotoregular); } return convertview; } static class viewholderitem { imageview imageannonce; textview titre; textview villeobj; textview prix; } }
Comments
Post a Comment