recyclerview-Custom Itemdecoration Detailed

Source: Internet
Author: User
public class Sectiondecoration extends Recyclerview.itemdecoration {

    @Override public
    void OnDraw (Canvas c, Recyclerview parent, Recyclerview.state State) {
        Super.ondraw (c, parent, state);

    @Override public
    void Getitemoffsets (Rect outrect, view view, Recyclerview parent, recyclerview.state state) {
        Super.getitemoffsets (Outrect, view, parent, State);
    }

2 methods are available

Method One

Custom itemdecoration public class Myitemdecoration extends Recyclerview.itemdecoration {@Override public void getIt Emoffsets (Rect outrect, view view, Recyclerview parent, recyclerview.state State) {System.out.println ("left ="
        + Outrect.left);
        System.out.println ("top =" + Outrect.top);
        System.out.println ("right =" + Outrect.right);
      System.out.println ("bottom =" + Outrect.bottom);
    Super.getitemoffsets (Outrect, view, parent, state); @Override public void Ondrawover (Canvas C, Recyclerview parent, recyclerview.state state) {Super.ondra
    Wover (c, parent, state); 
The default value for getitemoffsets parameter one outrect is 0:01-23 17:33:31.727 24556-24556/com.adnonstop.brvah = 0 01-23 17:33:31.727 24556-24556/com.adnonstop.brvah i/system.out:top = 0 01-23 17:33:31.727 24556-24556/
     
Com.adnonstop.brvah i/system.out:right = 0 01-23 17:33:31.727 24556-24556/com.adnonstop.brvah i/system.out:bottom = 0 Modify OUtrect @Override public void Getitemoffsets (Rect outrect, view view, Recyclerview parent, Recyclerview.state
        State) {Outrect.set (60, 60, 60, 60);
        System.out.println ("left =" + Outrect.left);
        System.out.println ("top =" + Outrect.top);
        System.out.println ("right =" + Outrect.right);
       System.out.println ("bottom =" + Outrect.bottom);     Super.getitemoffsets (Outrect, view, parent, state);
    Be sure to log off, because the parent class executes Outrect.set (0,0,0,0); The value 01-23 17:37:46.037 31451-31451/com.adnonstop.brvah i/system.out:left = outrect 01-23 17:37:46.037  M.adnonstop.brvah i/system.out:top = 01-23 17:37:46.037 31451-31451/com.adnonstop.brvah i/system.out:right = 60 01-23 17:37:46.037 31451-31451/com.adnonstop.brvah I/system.out:bottom = 60

method, similar to setting margin or padding for each item layout




Full definition

Linearlayoutmanager's split line public class Myitemdecoration extends Recyclerview.itemdecoration {private final drawable D

    rawable; 
    Public Myitemdecoration {drawable = contextcompat.getdrawable (context, r.drawable.item_divider);
     /** * If you only need to set the Linearlayoutmanager split line, then only need to set the Outrect.top value. * @param outrect * @param view * @param parent * @param state/@Override public void GetItem
         Offsets (Rect outrect, view view, Recyclerview parent, recyclerview.state State) {/** * Skip item with index 0
        */if (parent.getchildlayoutposition (view) < 1) {return;
    /** * Set the relative offset of the item */outrect.top = Drawable.getintrinsicheight (); /** * Remember: Start with an item with index 1 * * @param c * @param parent * @param state/@Over Ride public void Ondrawover (Canvas C, Recyclerview parent, recyclerview.state state) {

        int childcount = Parent.getchildcount ();

        int RIGHTV = Parent.getwidth ();
            for (int i = 0; i < ChildCount. i++) {View child = Parent.getchildat (i);

            Recyclerview.layoutparams params = (recyclerview.layoutparams) child.getlayoutparams ();

            int LEFTV = Parent.getpaddingleft () + child.getpaddingleft ();
            Drawn from the index of item 1.

            
            int bottomv = Child.gettop ()-params.topmargin;


            int TOPV = Bottomv-drawable.getintrinsicheight ();
            Drawable.setbounds (LEFTV, TOPV, Rightv, BOTTOMV);
        Drawable.draw (c); Drawable using shape drawing: Item_divider.xml <?xml version= "1.0" encoding= "Utf-8"?> <shape xmlns:android= "Htt" P://schemas.android.com/apk/res/android "android:shape=" Rectangle "> <size android:height=" 10dp "/> &L T;solid android:color= "@color/coloraccent"/> </shape>



When the total number of entries is 0 o'clock, the Itemdecotation method is not invoked.



When itemcount = 3, see the order in which the Itemdecoration method is executed

02-10 12:28:14.566 6626-6626/com.qingmu.footerrecyclerview i/sectiondecorationchild:getitemoffsets: = 1
02-10 12:28:14.566 6626-6626/com.qingmu.footerrecyclerview I/sectiondecorationchild:getitemoffsets: = 2
02-10 12:28:14.576 6626-6626/com.qingmu.footerrecyclerview I/sectiondecorationchild:getitemoffsets: = 3
02-10 12:28:14.576 6626-6626/com.qingmu.footerrecyclerview I/sectiondecorationchild:ondraw: = 3
02-10 12:28:15.026 6626-6626/com.qingmu.footerrecyclerview I/sectiondecorationchild:ondraw: = 3

It is obvious that the ItemCount Getitemoffsets is executed according to ItemCount, and then the ItemCount-1 secondary OnDraw is executed.


  Under normal circumstances params. TopMargin = 0, so bottomv = Child.gettop ();
  int bottomv = Child.gettop ()-params.topmargin;//this params. TopMargin is not a getitemoffsets outrect.top = Topgap;


/**
     * y value smaller, more on the
     * BOTTOMV destined to be greater than TOPV
     * *
     @param c
     * @param parent
     *
     @param
    State * * Override public
    void OnDraw (Canvas C, Recyclerview parent, recyclerview.state state) {









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.