Recyclerview as an upgraded version of the ListView GridView, but cannot specify a split line between item in XML
But this also has the advantage that we are more flexible to customize our split line, you can customize any type of Split line, let's look at
How to customize the split line between item
public void Additemdecoration (Itemdecoration decor) {
}
Recyclerview provides us with a additemdecoration approach that requires passing in Itemdecoration objects, and Itemdecoration
is the class where we implement the split line,
Itemdecoration is an abstract class, so we need to implement it,
public static abstract class Itemdecoration {public void OnDraw (Canvas C, Recyclerview parent, state state) {
OnDraw (c, parent); } @Deprecated public void OnDraw (Canvas C, Recyclerview parent) {} public void Ondr
Awover (Canvas C, Recyclerview parent, state State) {Ondrawover (c, parent);
} @Deprecated public void Ondrawover (Canvas C, Recyclerview parent) {} @Deprecated public void Getitemoffsets (Rect outrect, int itemposition, Recyclerview parent) {outrect.set (0, 0,
0, 0); } public void Getitemoffsets (Rect outrect, view view, Recyclerview parent, state state) {Getitemof
Fsets (Outrect, ((layoutparams) View.getlayoutparams ()). Getviewposition (), parent); }} You can see that there are only 6 methods in the class, and there are 3 ways Google has not advocated it. In fact, we look at the source code, do not advocate the use of the method is less than other methods of a parameter state state, the state records the RecyclerviewSome basic information, here do not delve into.
And our custom item split line really need to use the method actually only two, 1, draw split line public void OnDraw (Canvas C, Recyclerview parent, state state) {
} 2, set the size of the split line public void Getitemoffsets (Rect outrect, view view, Recyclerview parent, state state) { OK, next look at how to draw the split line and set its size:
Package moon.recyclerveiw;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.Rect;
Import Android.support.v7.widget.LinearLayoutManager;
Import Android.support.v7.widget.RecyclerView;
Import Android.util.TypedValue;
Import Android.view.View;
/** * Created by Moon.zhong on 2015/2/11. */public class Divideritemdecoration extends recyclerview.itemdecoration{/* * Recyclerview layout direction, default first assignment * For portrait layout * Recyclerview layout can be horizontal or vertical * horizontal and vertical corresponding division want to be different. * */private int morientation = Linearlayoutmanage
r.vertical;
The size of the split line between/** * Item, DEFAULT is 1 */private int mitemsize = 1;
/** * Draw the item split line brush, and set its properties * to draw the character split line */private Paint mpaint; /** * Construction method in the layout direction, you have to pass * @param context * @param orientation */Public divideritemdecoration (C
Ontext Context,int orientation) { This.morientation = orientation; if (orientation! = linearlayoutmanager.vertical && Orientation! = linearlayoutmanager.horizontal) {THR
ow new IllegalArgumentException ("Please pass in the correct parameters"); } mitemsize = (int) typedvalue.applydimension (mitemsize, Typedvalue.complex_unit_dip,context.getresources (). getDi
Splaymetrics ());
Mpaint = new Paint (Paint.anti_alias_flag);
Mpaint.setcolor (Color.Blue);
/* Set Fill */Mpaint.setstyle (Paint.Style.FILL); } @Override public void OnDraw (Canvas C, Recyclerview parent, recyclerview.state State) {if (morientat
Ion = = linearlayoutmanager.vertical) {drawvertical (c,parent);
}else {drawhorizontal (c,parent); }}/** * Draw portrait Item Split Line * @param canvas * @param parent */private void drawvertic
Al (Canvas Canvas,recyclerview parent) {Final int left = Parent.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 + mitemsize;
Canvas.drawrect (Left,top,right,bottom,mpaint); }}/** * Draw horizontal Item Split Line * @param canvas * @param parent */private void Drawhorizo
Ntal (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 + mitemsize;
Canvas.drawrect (Left,top,right,bottom,mpaint); }}/** * Sets the size of item split line * @param outrect * @param view * @param parent * @param State */@Override public void Getitemoffsets (Rect outrect, view view, Recyclerview parent, RECYCLERVIEW.S
Tate state) {if (morientation = = linearlayoutmanager.vertical) {outrect.set (0,0,0,mitemsize);
}else {outrect.set (0,0,mitemsize,0);
}}} The Mainactivity code is as follows: Package moon.recyclerveiw;
Import android.support.v7.app.ActionBarActivity;
Import Android.os.Bundle;
Import Android.support.v7.widget.LinearLayoutManager;
Import Android.support.v7.widget.RecyclerView; public class MainactiviTy extends actionbaractivity {private Recyclerview mrecyclerview;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mrecyclerview = (Recyclerview) Findviewbyid (r.id.id_recycle);
Linearlayoutmanager LayoutManager = new Linearlayoutmanager (this);
Layoutmanager.setorientation (linearlayoutmanager.vertical);
Mrecyclerview.setlayoutmanager (LayoutManager);
Mrecyclerview.additemdecoration (New Divideritemdecoration (This,layoutmanager.getorientation ()));
Mrecyclerview.setadapter (New Recycleradapter ());
}
}