android - horizontal recyclerView with wrap_content in vertical recyclerView -
in app have situation in have create horizontal recyclerview in vertical recyclerview. rows show casual details , in show horizontal recyclerview.
i have set height of horizontal recyclerview wrap_content not visible. when have added hardcoded height it, recyclerview visible. have searched lot regarding problem didn't got working or convincing solution.
here adapter class
public class homervadapter extends recyclerview.adapter<homervadapter.homeviewholder> { private arraylist<linkedhashmap<string, object>> data; private context ctx; private int selectedindex; public homervadapter(context ctx, arraylist<linkedhashmap<string, object>> val) { data = val; this.ctx = ctx; selectedindex = -1; } @override public homeviewholder oncreateviewholder(viewgroup parent, int viewtype) { layoutinflater inflater = ((activity) ctx).getlayoutinflater(); view view = inflater.inflate(r.layout.custom_home_recycler, null); return new homeviewholder(view); } @override public int getitemcount() { return data.size(); } @override public void onbindviewholder(final homeviewholder holder, final int position) { if (getitemcount() == position+1) { holder.categoryll.setvisibility(view.gone); holder.productll.setvisibility(view.visible); linearlayoutmanager manager = new linearlayoutmanager(ctx); manager.setorientation(linearlayoutmanager.horizontal); arraylist<subcategorydetails> list = new arraylist<>(); list.add(new subcategorydetails()); list.add(new subcategorydetails()); list.add(new subcategorydetails()); list.add(new subcategorydetails()); list.add(new subcategorydetails()); list.add(new subcategorydetails()); list.add(new subcategorydetails()); list.add(new subcategorydetails()); list.add(new subcategorydetails()); list.add(new subcategorydetails()); list.add(new subcategorydetails()); list.add(new subcategorydetails()); holder.subcatrv.setadapter(new promoproductadapter(list, holder.subcatrv)); holder.subcatrv.setlayoutmanager(manager); } else { holder.categoryll.setvisibility(view.visible); holder.productll.setvisibility(view.gone); if (selectedindex == position) { holder.subcatgrid.setvisibility(view.visible); showgrid(holder); } else { holder.subcatgrid.setvisibility(view.gone); } holder.cattr.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (selectedindex == position) { holder.subcatgrid.setvisibility(view.gone); selectedindex = -1; } else { selectedindex = position; showgrid(holder); } } }); } } private void showgrid(homeviewholder holder) { arraylist<subcategorydetails> list = new arraylist<>(); list.add(new subcategorydetails()); list.add(new subcategorydetails()); list.add(new subcategorydetails()); list.add(new subcategorydetails()); list.add(new subcategorydetails()); list.add(new subcategorydetails()); list.add(new subcategorydetails()); subcategorygridadapter adapter = new subcategorygridadapter(list); holder.subcatgrid.setadapter(adapter); holder.subcatgrid.setvisibility(view.visible); } class homeviewholder extends recyclerview.viewholder { recyclerview subcatrv; textview cattitletv, productnametv; imageview productiv; nonscrollablegridview subcatgrid; linearlayout categoryll, productll; tablerow cattr; public homeviewholder(view view) { super(view); subcatrv = (recyclerview) view.findviewbyid(r.id.subcatrv); productnametv = (textview) view.findviewbyid(r.id.productnametv); cattitletv = (textview) view.findviewbyid(r.id.cattitletv); productiv = (imageview) view.findviewbyid(r.id.productiv); subcatgrid = (nonscrollablegridview) view.findviewbyid(r.id.subcatgrid); categoryll = (linearlayout) view.findviewbyid(r.id.categoryll); productll = (linearlayout) view.findviewbyid(r.id.productll); cattr = (tablerow) view.findviewbyid(r.id.cattr); } } class subcategorygridadapter extends baseadapter { arraylist<subcategorydetails> subcatdata; public subcategorygridadapter(arraylist<subcategorydetails> subcatdata){ this.subcatdata = subcatdata; } @override public int getcount() { return subcatdata.size(); } @override public object getitem(int position) { return null; } @override public view getview(int position, view convertview, viewgroup parent) { if (convertview == null) { convertview = ((activity) ctx).getlayoutinflater().inflate(r.layout.custom_sub_cat_grid, null, true); } return convertview; } @override public long getitemid(int position) { return 0; } } class promoproductadapter extends recyclerview.adapter<promoproductadapter.promoholder> { arraylist<subcategorydetails> data; recyclerview horizontalrv; public promoproductadapter(arraylist<subcategorydetails> data, recyclerview horizontalrv){ this.data = data; this.horizontalrv = horizontalrv; } @override public promoholder oncreateviewholder(viewgroup parent, int viewtype) { layoutinflater inflater = ((activity) ctx).getlayoutinflater(); view view = inflater.inflate(r.layout.custom_promoted_product, null); if (horizontalrv != null) { int height = view.getmeasuredheight(); horizontalrv.getlayoutparams().height = height; } return new promoholder(view); } @override public int getitemcount() { return data.size(); } @override public void onbindviewholder(promoholder holder, int position) { } class promoholder extends recyclerview.viewholder { public promoholder(view view) { super(view); } } }
}
please me problem solved.
i solved on own. have used custom linearlayoutmanager have found link.
public class mylinearlayoutmanager extends linearlayoutmanager { public mylinearlayoutmanager(context context) { super(context); } private int[] mmeasureddimension = new int[2]; @override public void onmeasure(recyclerview.recycler recycler, recyclerview.state state, int widthspec, int heightspec) { final int widthmode = view.measurespec.getmode(widthspec); final int heightmode = view.measurespec.getmode(heightspec); final int widthsize = view.measurespec.getsize(widthspec); final int heightsize = view.measurespec.getsize(heightspec); measurescrapchild(recycler, 0, view.measurespec.makemeasurespec(0, view.measurespec.unspecified), view.measurespec.makemeasurespec(0, view.measurespec.unspecified), mmeasureddimension); int width = mmeasureddimension[0]; int height = mmeasureddimension[1]; switch (widthmode) { case view.measurespec.exactly: case view.measurespec.at_most: width = widthsize; break; case view.measurespec.unspecified: } switch (heightmode) { case view.measurespec.exactly: case view.measurespec.at_most: height = heightsize; break; case view.measurespec.unspecified: } setmeasureddimension(width, height); } private void measurescrapchild(recyclerview.recycler recycler, int position, int widthspec, int heightspec, int[] measureddimension) { view view = recycler.getviewforposition(position); if (view != null) { recyclerview.layoutparams p = (recyclerview.layoutparams) view.getlayoutparams(); int childwidthspec = viewgroup.getchildmeasurespec(widthspec, getpaddingleft() + getpaddingright(), p.width); int childheightspec = viewgroup.getchildmeasurespec(heightspec, getpaddingtop() + getpaddingbottom(), p.height); view.measure(childwidthspec, childheightspec); measureddimension[0] = view.getmeasuredwidth(); measureddimension[1] = view.getmeasuredheight(); recycler.recycleview(view); } }}
just have add linearlayoutmanager recyclerview that's it.
thanks cheers!!!
Comments
Post a Comment