Recycler does not directly provide the ability to set the item spacing, but rather provides a more powerful base class itemdecoration. Class as its name, this class is the decoration of item. It can be either a spacing between item or a separator line between item, or even a different drawing for each item's edge.
Itemdecoration itself is a virtual class, and we can only inherit it when we use it.
Looking at a simple version, this version of Itemdecoration only provides a single item spacing.
ImportAndroid.graphics.Rect;ImportAndroid.support.v7.widget.RecyclerView;ImportAndroid.util.Log;ImportAndroid.view.View;/*** Created by Fishbonelsy on 2016/6/25.*/ Public classDividedecorationextendsrecyclerview.itemdecoration{@Override Public voidgetitemoffsets (Rect outrect, view view, Recyclerview parent, recyclerview.state state) {if(Parent.getchildadapterposition (view) < Parent.getadapter (). GetItemCount ()-1) {Outrect.bottom= 30; } }}
In the example above, we will leave a 30-pixel interval under each item, and add a judgment statement, that is, the bottom of the last item is no longer spaced. From the above example extrapolate, we can add different blanks for different item.
--------------------------------------------------------------------------------------------------------------- -------
In addition to setting the interval, we can also draw a divider line:
ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Rect;Importandroid.graphics.drawable.Drawable;ImportAndroid.os.Build;ImportAndroid.support.v7.widget.RecyclerView;ImportAndroid.view.View;/*** Created by Fishbonelsy on 2016/6/25.*/ Public classDrawerdecorationextendsrecyclerview.itemdecoration {drawable mdivider; Publicdrawerdecoration (Context context) {if(Build.VERSION.SDK_INT >=Build.version_codes. LOLLIPOP) {Mdivider= Context.getresources (). getdrawable (R.mipmap.ic_launcher,NULL); }Else{Mdivider=context.getresources (). getdrawable (R.mipmap.ic_launcher); }} @Override Public voidOnDraw (Canvas C, Recyclerview parent, recyclerview.state state) {intleft =Parent.getpaddingleft (); intright = Parent.getwidth ()-parent.getpaddingright (); intChildCount =Parent.getchildcount (); for(inti = 0; i < ChildCount; i++) {View child=Parent.getchildat (i); Recyclerview.layoutparams params=(Recyclerview.layoutparams) child.getlayoutparams (); inttop = Child.getbottom () +Params.bottommargin; intBottom = top +mdivider.getintrinsicheight (); Mdivider.setbounds (left, top, right, bottom); Mdivider.draw (c); }} @Override Public voidgetitemoffsets (Rect outrect, view view, Recyclerview parent, recyclerview.state State) {Outrect.bottom=mdivider.getintrinsicheight (); }}
From the above example, we can see that the drawing of the dividing line is essentially done in a loop. In other words, we can draw a different divider for each item. We can draw on the canvas ourselves, or we can draw a drawable directly onto the canvas, and the code above is to draw a drawable directly onto the canvas.
In this case, our outrect.bottom must acquire the drawable of the real height. Such a class gives us a great deal of flexibility in the custom of dividing lines. Perhaps creating a well-experienced list control provides a lot of convenience.
done~
ImportAndroid.content.Context;
ImportAndroid.graphics.Canvas;
ImportAndroid.graphics.Rect;
Importandroid.graphics.drawable.Drawable;
ImportAndroid.os.Build;
ImportAndroid.support.v7.widget.RecyclerView;
ImportAndroid.view.View;
/**
* Created by Fishbonelsy on 2016/6/25.
*/
Public classDrawerdecorationextendsrecyclerview.itemdecoration {
DrawableMdivider;
PublicDrawerdecoration (ContextContext) {
off(Build.version.Sdk_int>= Build.version_codes.LOLLIPOP) {
Mdivider= Context.getresources (). getdrawable (R.mipmap.Ic_launcher,NULL);
}Else{
Mdivider= Context.getresources (). getdrawable (R.mipmap.Ic_launcher);
}
}
@Override
Public voidOnDraw (Canvas C, Recyclerview parent, recyclerview.state state) {
intleft = Parent.getpaddingleft ();
intright = Parent.getwidth ()-parent.getpaddingright ();
intChildCount = Parent.getchildcount ();
for(inti =0; i < ChildCount; i++) {
View child = Parent.getchildat (i);
Recyclerview.layoutparams params = (recyclerview.layoutparams) child.getlayoutparams ();
inttop = Child.getbottom () + params.BottomMargin;
intBottom = top +Mdivider. Getintrinsicheight ();
Mdivider. SetBounds (left, top, right, bottom);
Mdivider. Draw (c);
}
}
@Override
Public voidGetitemoffsets (Rect outrect, view view, Recyclerview parent, recyclerview.state state) {
Outrect.Bottom=Mdivider. Getintrinsicheight ();
}
}
Recyclerview (b)--itemdecoration