100 line Android code customize a streaming layout-flowlayout

Source: Internet
Author: User

First look at the hand Amoy HD-Product Details-Select the Product Properties page UI

There are many sizes, and the size of the view required to show each size is different (mostly width), so the number of rows and rows required to show all sizes is not determined until you pull the data from the server side, so you cannot use the GridView or ListView directly.

What if I use LinearLayout?

A linearlayout can only display a row, if you want to show more than one row, then each row will be a new linearlayout out, but also have to calculate how many sizes each linearlayout can hold the corresponding view, the implementation will be more complex.

In fact, to achieve this function, only need to learn from the CSS3 Flex-box on it.

To implement an Android version of the Flexbox, the principle is very simple, in order to be consistent with the Android naming conventions, we call it FlowLayout .

    1. Start by creating a new FlowLayout class that inherits fromViewGroup
    2. onMeasureCalculate height According to child's views FlowLayout in
    3. Layout of a child's views in onLayout

Here are just a few of the core code snippets, the full code has been put on GitHub-androidflowlayout, welcome fork.

Calculate the height of flowlayout in Onmeasure
//Traverse all sub-view for(inti =0, ChildCount = Getchildcount (); i < ChildCount; ++i) {View Childview = Getchildat (i);//Measure the child view and gets its width and heightLayoutparams childlayoutparams = Childview.getlayoutparams ();            Childview.measure (Getchildmeasurespec (widthmeasurespec, Paddingleft + paddingright, childLayoutParams.width), Getchildmeasurespec (Heightmeasurespec, Paddingtop + paddingbottom, childlayoutparams.height));intChildwidth = Childview.getmeasuredwidth ();intChildheight = Childview.getmeasuredheight ();//Calculates the height of the current row (the highest of all child view in the current row)Lineheight = Math.max (Childheight, lineheight);//Put the current child view on the right side of the previous child view, or wrap if it doesn't fit    if(Childleft + childwidth + paddingright > Mywidth)        {childleft = paddingleft;        Childtop + = mverticalspacing + lineheight;    Lineheight = Childheight; }Else{Childleft + = childwidth + mhorizontalspacing; }}intWantedheight = childtop + lineheight + paddingbottom;//Calculate the height required for FlowLayoutSetmeasureddimension (Mywidth, Resolvesize (Wantedheight, Heightmeasurespec));
Layout a child's views in onlayout

The code onMeasure is very similar and only needs to be placed in the specified position based on the width and height of the child view.

 for(inti =0, ChildCount = Getchildcount (); i < ChildCount; ++i) {View Childview = Getchildat (i);if(childview.getvisibility () = = View.gone) {Continue; }intChildwidth = Childview.getmeasuredwidth ();intChildheight = Childview.getmeasuredheight (); Lineheight = Math.max (Childheight, lineheight);if(Childleft + childwidth + paddingright > Mywidth)        {childleft = paddingleft;        Childtop + = mverticalspacing + lineheight;    Lineheight = Childheight; }//Key codeChildview.layout (Childleft, childtop, Childleft + childwidth, childtop + childheight); Childleft + = Childwidth + mhorizontalspacing;}

The full version of the code has been put into the github-flowlayout, the AAR package has been uploaded to the Bintray, the use of the way is very simple, just need to add a dependency in the project to the corresponding build.gradle .

compile‘com.liangfeizc:flowlayout:1.0.0@aar‘
PutAARThe package is uploaded toJcenter

Refer to the publishing Gradle Android Library to jcenter for specific practices
Packaging scripts can refer to Flowlayout/build.gradle

Please leave a message if you have questions. よろしくお いします

100 line Android code customize a streaming layout-flowlayout

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.