Used to the small friends of the ListView are known. The ListView has such an attribute Android:divider, which is used to set the Cut line drop properties between each item. The question comes, so recyclerview the control has this property, try it, without this property. But Recyclerview has this method mlinearrecycler.additemdecoration (), used to add the cutting line drops. Here are some of the source code:
Public Static Abstract classitemdecoration { Public void OnDraw(Canvas C, Recyclerview parent, State State) {OnDraw (c, parent);} Public void Ondrawover(Canvas C, Recyclerview parent, State State) {Ondrawover (c, parent);} @Deprecated Public void getitemoffsets(Rect Outrect,intItemposition,recyclerview parent) {Outrect.Set(0,0,0,0);} Public void getitemoffsets(Rect Outrect, View view,recyclerview parent, State State) {Getitemoffsets (Outrect, ((layoutparams) View.getlayoutparams ()). Getviewlayoutposition (), parent);} }
Recyclerview in the call Additemdecoration () will go to draw decoration, through the source code we will find that will invoke the inside of the OnDraw () and Ondrawover () method. The Getitemoffsets () is set to a certain offset for each item. Mainly used to draw decorator.
Let's take a look at the implementation of this class, such as the following code:
PackageRecyclerview.hy.com.myrecyclerview.decoration;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Rect;Importandroid.graphics.drawable.Drawable;ImportAndroid.support.v7.widget.LinearLayoutManager;ImportAndroid.support.v7.widget.RecyclerView;ImportAndroid.view.View;ImportRECYCLERVIEW.HY.COM.MYRECYCLERVIEW.R;/** * Created by Ying on 2016/2/16. */ Public class listitemdecoration extends recyclerview. itemdecoration { PrivateDrawable mdrawable;Private Final Static intDefault_orentation = linearlayoutmanager.vertical;Private intMorientation; Public listitemdecoration(Context context,intOrientation) {if(Orientation! = Linearlayoutmanager.horizontal && Orientation! = linearlayoutmanager.vertical) {//Method One: Assuming the layout display direction is not set, the default setting is vertical This. morientation = default_orentation;//Method Two: Throws an exception, prompting to set the layout direction //throw new IllegalArgumentException ("Invalid orientation");}Else{ This. morientation = orientation; }//Set cut line Stylemdrawable = Context.getresources (). getdrawable (R.drawable.divider); }@Override Public void OnDraw(Canvas C, Recyclerview parent, recyclerview.state State) {if(morientation = = linearlayoutmanager.horizontal) {Drawhorizontal (c, parent); }Else{Drawvertical (c, parent); } }Private void Drawhorizontal(Canvas C, recyclerview parent) {inttop = Parent.getpaddingtop ();intBottom = Parent.getheight ()-Parent.getpaddingbottom ();intChildCount = Parent.getchildcount (); for(inti =0; i < ChildCount; i++) {View child = Parent.getchildat (i); Recyclerview.layoutparams params = (recyclerview.layoutparams) child.getlayoutparams ();intleft = Child.getright () + Params.rightmargin;intright = left + mdrawable.getintrinsicheight (); Mdrawable.setbounds (left, top, right, bottom); Mdrawable.draw (c); } }Private void drawvertical(Canvas C, recyclerview parent) {intleft = Parent.getpaddingleft ();intright = Parent.getwidth ()-parent.getpaddingright ();intChildCount = Parent.getchildcount (); for(inti =0; i < ChildCount; i++) {View child = Parent.getchildat (i); Android.support.v7.widget.RecyclerView v =NewAndroid.support.v7.widget.RecyclerView (Parent.getcontext ()); Recyclerview.layoutparams params = (recyclerview.layoutparams) child. Getlayoutparams ();inttop = Child.getbottom () + Params.bottommargin;intBottom = top + mdrawable.getintrinsicheight (); Mdrawable.setbounds (left, top, right, bottom); Mdrawable.draw (c); } }@Override Public void getitemoffsets(Rect outrect, view view, Recyclerview parent, recyclerview.state State) {if(morientation = = default_orentation) {Outrect.set (0,0,0, Mdrawable.getintrinsicheight ()); }Else{Outrect.set (0,0, Mdrawable.getintrinsicwidth (),0); } } }
This implements the class of the cutting line, and we simply add this sentence to the activity to:
mLinearRecycler.addItemDecoration(new ListItemDecoration(this, LinearLayoutManager.VERTICAL));
Take a look:
Suppose you want to set other style drop lines just to make changes in this sentence:
mDrawable = context.getResources().getDrawable(R.drawable.divider);
About Recyclerview implementation of the GridView and waterfall flow effect is not affixed to the code. These codes are in the demo.
Take a look:
Gridview:
Waterfall Flow:
About waterfall Stream Background Click Color, suppose there is doubt, can participate in my drop also has a blog Android simple Change button color scheme.
Project Code: Demo
If you have any questions. Remember to give me a message Oh ~ Technical Exchange ~ ~ like drops, the top of the chant!
Android Recyclerview Simple to use (iii)