Recyclerview (b)--itemdecoration

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.