android - Put different background colors for each item in a ViewHolder -


i have created gridview using viewholder adapter include in each item image , text.
code correctly working , display smooth, want add different background colors each item. have checked severals posts this one or this one, cannot manage make work.

the gridviewadapter created in main activity:

private list<gridviewitem> mitems;    // gridview items list private gridviewadapter madapter;    // gridview adapter 

and used in oncreate follows:

mitems = new arraylist<gridviewitem>(); resources resources = getresources();  mitems.add(new gridviewitem(resources.getdrawable(r.drawable.truck_package), getstring(r.string.drop_package))); mitems.add(new gridviewitem(resources.getdrawable(r.drawable.man_package), getstring(r.string.pick_package))); mitems.add(new gridviewitem(resources.getdrawable(r.drawable.register), getstring(r.string.register))); madapter = new gridviewadapter(this, mitems); // initialize gridview final gridview gridview = (gridview) this.findviewbyid(r.id.gridview); gridview.setadapter(madapter); 

my gridviewadapter works follows:

public class gridviewadapter extends baseadapter {     private context mcontext;     private list<gridviewitem> mitems;       public gridviewadapter(context context, list<gridviewitem> items) {         mcontext = context;         mitems = items;     }      @override     public int getcount() {         return mitems.size();     }      @override     public object getitem(int position) {         return mitems.get(position);     }      @override     public long getitemid(int position) {         return position;     }      @override     public view getview(int position, view convertview, viewgroup parent) {         viewholder viewholder;          if(convertview == null) {             // inflate gridview item layout             layoutinflater inflater = layoutinflater.from(mcontext);             convertview = inflater.inflate(r.layout.gridview_item, parent, false);              // initialize view holder             viewholder = new viewholder();             viewholder.ivicon = (imageview) convertview.findviewbyid(r.id.ivicon);             viewholder.tvtitle = (textview) convertview.findviewbyid(r.id.tvtitle);             convertview.settag(viewholder);         } else {             // recycle inflated view             viewholder = (viewholder) convertview.gettag();         }          // update item view         gridviewitem item = mitems.get(position);         viewholder.ivicon.setimagedrawable(item.icon);         viewholder.tvtitle.settext(item.title);          return convertview;     }      /**      * view holder design pattern prevents using findviewbyid()      * repeatedly in getview() method of adapter.      *      * @see ://developer.android.com/training/improving-layouts/smooth-scrolling.html#viewholder      */     private static class viewholder {         imageview ivicon;         textview tvtitle;     } } 

finally, each item of gridview defined follows:

public class gridviewitem {     public final drawable icon;       // drawable listview item imageview     public final string title;        // text gridview item title       public gridviewitem(drawable icon, string title) {         this.icon = icon;         this.title = title;      } } 

my guess should add "color" variable in gridviewitem. use viewholder.itemview.setbackgroundcolor(color) in gridviewadapter per both text , image. think right approach? if so, how it?

finally, , not totally sure if optimal way use gridview, appreciate know if there better way implement it. thanks!

use parent layout id of r.layout.gridview_item file. assume have line layout parent layout in file use below way.

@override     public view getview(int position, view convertview, viewgroup parent) {         viewholder viewholder;          if(convertview == null) {             // inflate gridview item layout             layoutinflater inflater = layoutinflater.from(mcontext);             convertview = inflater.inflate(r.layout.gridview_item, parent, false);              // initialize view holder             viewholder = new viewholder();             viewholder.ivicon = (imageview) convertview.findviewbyid(r.id.ivicon);             viewholder.tvtitle = (textview) convertview.findviewbyid(r.id.tvtitle);             viewholder.line_list_item = (linearlayout) convertview.findviewbyid(r.id.line_list_item);             convertview.settag(viewholder);         } else {             // recycle inflated view             viewholder = (viewholder) convertview.gettag();         }          // update item view         gridviewitem item = mitems.get(position);         viewholder.ivicon.setimagedrawable(item.icon);         viewholder.tvtitle.settext(item.title);         if (position % 2 == 0) {                 viewholder.line_main.setbackgroundcolor(contextcompat.getcolor(mcontext, r.color.colorprimarydark));             } else {                 viewholder.line_main.setbackgroundcolor(contextcompat.getcolor(mcontext, r.color.colorprimary));             }          return convertview;     } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -