Android recyclerview using Gridlayoutmanager spacing settings

Source: Internet
Author: User

To set the spacing using Recyclerview, you need to override the Recyclerview.itemdecoration class. Like the next need to implement, the spacing is only between the middle of the lattice and the bottom of the format there.

Paste_image.png

The implementation is simple because the effect is that each row has 3 squares, as long as the first format of each row has a left spacing of 0. The rest sets the left and bottom distances.

The code is as follows:

 Public classSpaceitemdecoration extends Recyclerview.itemdecoration {Private intspace;  PublicSpaceitemdecoration (intspace) {         This. Space =space; } @Override Public voidgetitemoffsets (Rect outrect, view view, Recyclerview parent, recyclerview.state state) {//not the first grid has a left and bottom spacingOutrect.left =space; Outrect.bottom=space; //since each row has only 3, so the first is a multiple of 3, the left margin is set to 0        if(parent.getchildlayoutposition (view)%3==0) {Outrect.left=0; }    }}

Android recyclerview using Gridlayoutmanager spacing settings

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.