The Recycleview split line needs to be defined by itself and is not by default. The code is as follows
Package Com.ipd.east.eastapplication.adapter;import Android.content.context;import Android.content.res.TypedArray ; Import Android.graphics.canvas;import Android.graphics.paint;import Android.graphics.rect;import Android.graphics.drawable.drawable;import Android.support.v4.content.contextcompat;import Android.support.v7.widget.linearlayoutmanager;import Android.support.v7.widget.recyclerview;import Android.view.view;public class Recycleviewdivider extends Recyclerview.itemdecoration {private Paint mpaint; Private drawable Mdivider; private int mdividerheight = 2;//Split line height, default is 1px private int morientation;// The direction of the list: Linearlayoutmanager.vertical or linearlayoutmanager.horizontal private static final int[] Attrs = new Int[]{android . R.attr.listdivider}; /** * Default Split line: Height is 2px, color is gray * * @param context * @param orientation List Direction */Public Recycleviewdivider ( context context, int orientation) {if (Orientation! = linearlayoutmanager.vertical && OrientatIon! = Linearlayoutmanager.horizontal) {throw new IllegalArgumentException ("Please enter the correct parameters! "); } morientation = orientation; Final TypedArray a = Context.obtainstyledattributes (attrs); Mdivider = a.getdrawable (0); A.recycle (); }/** * Custom Split Line * * @param context * @param orientation list direction * @param Drawableid Split Line picture */PU Blic Recycleviewdivider (context context, int orientation, int drawableid) {This (context, orientation); Mdivider = contextcompat.getdrawable (context, Drawableid); Mdividerheight = Mdivider.getintrinsicheight (); }/** * Custom Split Line * * @param context * @param orientation list direction * @param dividerheight Split Line Height * @p Aram Dividercolor Split Line Color */public Recycleviewdivider (context context, int orientation, int dividerheight, int divi Dercolor) {This (context, orientation); Mdividerheight = Dividerheight; Mpaint = new Paint (paint.anti_alias_flag); Mpaint.setcolor (Dividercolor); Mpaint.setstyle (Paint.Style.FILL); }//Get Split line size @Override public void getitemoffsets (Rect outrect, view view, Recyclerview parent, Recyclerview.state State) {Super.getitemoffsets (Outrect, view, parent, state); Outrect.set (0, 0, 0, mdividerheight); }//Draw split line @Override public void OnDraw (Canvas C, Recyclerview parent, recyclerview.state state) {Super.on Draw (c, parent, state); if (morientation = = linearlayoutmanager.vertical) {drawvertical (c, parent); } else {Drawhorizontal (c, parent); }}//Draw landscape item split line private void drawhorizontal (canvas canvas, Recyclerview parent) {Final int left = par Ent.getpaddingleft (); Final int right = Parent.getmeasuredwidth ()-parent.getpaddingright (); Final int childsize = Parent.getchildcount (); for (int i = 0; i < childsize; i++) {final View child = Parent.getchilDAt (i); Recyclerview.layoutparams layoutparams = (recyclerview.layoutparams) child.getlayoutparams (); Final int top = Child.getbottom () + Layoutparams.bottommargin; final int bottom = top + mdividerheight; if (Mdivider! = null) {Mdivider.setbounds (left, top, right, bottom); Mdivider.draw (canvas); } if (Mpaint! = null) {Canvas.drawrect (left, top, right, bottom, mpaint); }}}//Draw portrait item split line private void drawvertical (canvas canvas, Recyclerview parent) {Final int top = Parent.getpaddingtop (); final int bottom = Parent.getmeasuredheight ()-Parent.getpaddingbottom (); Final int childsize = Parent.getchildcount (); for (int i = 0; i < childsize; i++) {final View child = Parent.getchildat (i); Recyclerview.layoutparams layoutparams = (recyclerview.layoutparams) child.getlayoutparams (); Final int left = Child.getright () + Layoutparams.rightmargin; Final int right = left + mdividerheight; if (Mdivider! = null) {Mdivider.setbounds (left, top, right, bottom); Mdivider.draw (canvas); } if (Mpaint! = null) {Canvas.drawrect (left, top, right, bottom, mpaint); } } }}
If you use it in activity, you should use it
Rvxunjiadanchakanbaojia.additemdecoration (New Recycleviewdivider (Getbasecontext (), Linearlayoutmanager.vertical,20,getresources (). GetColor (R.color.gray));
The ListView split line looks like this
There is a split line between each item item in the ListView, and setting android:footerdividersenabled indicates whether the split line is displayed, which defaults to true.
1. Do not display the split line as long as you add android:footerdividersenabled= "false" to the ListView control.
[HTML]View PlainCopy
- < listview &NBSP;&NBSP;
- android:id = " @+id/local_groups_list " &NBSP;&NBSP;
- android:layout_width = " match_parent " &NBSP;&NBSP;
- android:layout_height = " wrap_content " &NBSP;&NBSP;
- android:footerdividersenabled="false" />
2. Change the split line color and width of the ListView, you need to define the Android:divider and android:dividerheight properties in the layout.
[HTML]View PlainCopy
- < listview &NBSP;&NBSP;
- android:id = " @+id/local_groups_list " &NBSP;&NBSP;
- android:layout_width = " match_parent " &NBSP;&NBSP;
- android:layout_height = " wrap_content " &NBSP;&NBSP;
- android:divider = " @color/divider_color " &NBSP;&NBSP;
- android:dividerheight="1px" />
ListView Split Line, Recycleview split Line