android - GridLayoutManager same background for each row -
i have been trying project in link. working fine. got output also. enhance don't know do. having background , need keep background each row of gridlayoutmanager in recyclerview. please me on this. , in advance
mainactivity.java public class mainactivity extends appcompatactivity { recyclerview recyclerview; context context; recyclerview.adapter recyclerview_adapter; recyclerview.layoutmanager recyclerviewlayoutmanager; string[] numbers = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); context = getapplicationcontext(); recyclerview = (recyclerview) findviewbyid(r.id.recycler_view1); recyclerviewlayoutmanager = new gridlayoutmanager(context, 2); recyclerview.setlayoutmanager(recyclerviewlayoutmanager); recyclerview_adapter = new recyclerviewadapter(context,numbers); recyclerview.setadapter(recyclerview_adapter); } } recyclerviewadapter.java public class recyclerviewadapter extends recyclerview. adapter<recyclerviewadapter.viewholder>{ string[] values; context context1; public recyclerviewadapter(context context2,string[] values2){ values = values2; context1 = context2; } public static class viewholder extends recyclerview.viewholder{ public textview textview; public viewholder(view v){ super(v); textview = (textview) v.findviewbyid(r.id.textview1); } } @override public recyclerviewadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype){ view view1 = layoutinflater.from(context1).inflate (r.layout.recycler_view_items,parent,false); viewholder viewholder1 = new viewholder(view1); return viewholder1; } @override public void onbindviewholder(viewholder vholder, int position){ vholder.textview.settext(values[position]); vholder.textview.setbackgroundcolor(color.cyan); vholder.textview.settextcolor(color.blue); } @override public int getitemcount(){ return values.length; } }
easy hack : u can use linearlayout manager instead of gridlayoutmanager. u can change item layout (layout each item in adpater ie:recycler_view_items)
something this
<linearlayout background: set ur image> <linearlayout> // first item </linearlayout> <linearlayout> //2nd item </linearlayout> <linearlayout>
and change adapter set values accordingly. requires changing layout , adapter codes. work u u can set background each row
(ps: each row considered single item)
Comments
Post a Comment