android - Dividers didn't display in RecyclerView after addItemDecoration() called -
a recyclerview
nested in cardview
. tried call additemdecoration()
add dividers recycylerview
,but didn't work.
<android.support.v7.widget.cardview xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" card_view:cardbackgroundcolor="@color/white_color" android:layout_marginleft="10dp" android:layout_marginright="10dp" card_view:cardelevation="3dp"> <android.support.v4.widget.swiperefreshlayout android:id="@+id/swipe_saleproduct_list" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.recyclerview android:id="@+id/recycler_saleproduct_list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:visibility="visible" /> </android.support.v4.widget.swiperefreshlayout> </android.support.v7.widget.cardview>
the java code:
mswipe = (swiperefreshlayout)view.findviewbyid(r.id.swipe_saleproduct_list); mrecyclerview= (recyclerview) view.findviewbyid(r.id.recycler_saleproduct_list); mrecyclerview.setlayoutmanager(new linearlayoutmanager(uiutils.getcontext())); mrecyclerview.additemdecoration(new divideritemdecoration(uiutils.getcontext(), divideritemdecoration.vertical_list)); saleproductlistadapter saleproductlistadapter = new saleproductlistadapter(uiutils.getcontext(), mrecyclerview, list, mswipe); mrecyclerview.setadapter(saleproductlistadapter);
this below example, take @ it:
public class divideritemdecoration extends recyclerview.itemdecoration { private static final int[] attrs = new int[]{ android.r.attr.listdivider }; public static final int horizontal_list = linearlayoutmanager.horizontal; public static final int vertical_list = linearlayoutmanager.vertical; private drawable mdivider; private int morientation; public divideritemdecoration(context context, int orientation) { final typedarray = context.obtainstyledattributes(attrs); mdivider = a.getdrawable(0); a.recycle(); setorientation(orientation); } public void setorientation(int orientation) { if (orientation != horizontal_list && orientation != vertical_list) { throw new illegalargumentexception("invalid orientation"); } morientation = orientation; } @override public void ondraw(canvas c, recyclerview parent) { if (morientation == vertical_list) { drawvertical(c, parent); } else { drawhorizontal(c, parent); } } public void drawvertical(canvas c, recyclerview parent) { final int left = parent.getpaddingleft(); final int right = parent.getwidth() - parent.getpaddingright(); final int childcount = parent.getchildcount(); (int = 0; < childcount; i++) { final view child = parent.getchildat(i); final recyclerview.layoutparams params = (recyclerview.layoutparams) child .getlayoutparams(); final int top = child.getbottom() + params.bottommargin; final int bottom = top + mdivider.getintrinsicheight(); mdivider.setbounds(left, top, right, bottom); mdivider.draw(c); } } public void drawhorizontal(canvas c, recyclerview parent) { final int top = parent.getpaddingtop(); final int bottom = parent.getheight() - parent.getpaddingbottom(); final int childcount = parent.getchildcount(); (int = 0; < childcount; i++) { final view child = parent.getchildat(i); final recyclerview.layoutparams params = (recyclerview.layoutparams) child .getlayoutparams(); final int left = child.getright() + params.rightmargin; final int right = left + mdivider.getintrinsicheight(); mdivider.setbounds(left, top, right, bottom); mdivider.draw(c); } } @override public void getitemoffsets(rect outrect, int itemposition, recyclerview parent) { if (morientation == vertical_list) { outrect.set(0, 0, 0, mdivider.getintrinsicheight()); } else { outrect.set(0, 0, mdivider.getintrinsicwidth(), 0); } } }
and here fragment
, works well, divider
clear , thin:
public static class recyclerviewfragment extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view root = inflater.inflate(r.layout.fragment_recyclerview, container, false); recyclerview recyclerview = (recyclerview) root.findviewbyid(r.id.recycler_view); recyclerview.sethasfixedsize(true); recyclerview.setitemanimator(new defaultitemanimator()); recyclerview.setlayoutmanager(new linearlayoutmanager(getactivity())); recyclerview.additemdecoration(new divideritemdecoration(getactivity(), divideritemdecoration.vertical_list)); recyclerviewadapter adapter = new recyclerviewadapter(getactivity(), getresources() .getstringarray(r.array.countries)); recyclerview.setadapter(adapter); return root; } }
i hope inspired.
Comments
Post a Comment