Android Custom Streaming layout

Source: Internet
Author: User



Flow layout, the advantage is that the parent layout can automatically determine whether children need to change the line, when the need to change line, you can achieve the effect of the page version of the label. Today is simply a custom flow layout.

Specific effects:




Principle:
Actually very simple, Measure Layout. It takes just two steps to get it done. Complete manual go to measure Layout.
Let's take a look at the code.
The explanation is commented out in the code because the format does not conform to the code format because of the blog that is written for the known notes. We can see the specific source code. And finally the source code.
1.Measure Measurement

@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {

int widthsize = measurespec.getsize (Widthmeasurespec);
int widthmode = Measurespec.getmode (Widthmeasurespec);

int heightsize = measurespec.getsize (Heightmeasurespec);
int heightmode = Measurespec.getmode (Heightmeasurespec);

int lineheight = 0;
int linewidth = 0;

int width = 0;
int height = 0;

int childCount = Getchildcount ();

LOG.I ("Test", Getpaddingleft () + "==right=" +getpaddingright ());

for (int i = 0; i < ChildCount; i++) {
View Childview = Getchildat (i);
Measurechild (Childview, Widthmeasurespec, Heightmeasurespec);
Marginlayoutparams params = (marginlayoutparams) childview.getlayoutparams ();

int childwidth = childview.getmeasuredwidth () + Params.leftmargin + params.rightmargin;

int childheight = childview.getmeasuredheight () + Params.topmargin + params.bottommargin;


if ((LineWidth + childwidth) > Widthsize-getpaddingleft ()-getpaddingright ()) {
width = Math.max (width, linewidth);
LineWidth = Childwidth;
Height + = lineheight;
Lineheight = Childheight;
}else {
LineWidth + = Childwidth;
Lineheight = Math.max (lineheight, childheight);
}


if (i = = childCount-1) {
width = Math.max (width, linewidth);
Height + = lineheight;
}
}

Height + = getpaddingtop () + Getpaddingbottom ();

Setmeasureddimension (Widthmode = = Measurespec.exactly?widthsize:width,
Heightmode = = measurespec.exactly?heightsize:height);
}




2.onLayout layout
@Override
protected void OnLayout (Boolean A, int l, int t, int r, int b) {

Childviewlist.clear ();

int childCount = Getchildcount ();
int width = getwidth ();
int linewidth = 0;
int lineheight = 0;

list<view> lineviews = new arraylist<view> ();
for (int i = 0; i < ChildCount; i++) {
View Childview = Getchildat (i);
Marginlayoutparams params = (marginlayoutparams) childview.getlayoutparams ();

int childwidth = childview.getmeasuredwidth () + Params.leftmargin + params.rightmargin;
int childheight = childview.getmeasuredheight () + Params.topmargin + params.bottommargin;

if (linewidth + childwidth > Width-getpaddingleft ()-getpaddingright ()) {

Childviewlist.add (lineviews);
Lineviews = new arraylist<view> ();

if (i = = 0) {
Lineheight + = Getpaddingtop ();
}else if (i== childCount-1) {
Lineheight + = Getpaddingbottom ();
}
This.lineHeight.add (lineheight);

lineheight = 0;
LineWidth = 0;
}

LineWidth + = Childwidth;
Lineheight = Math.max (lineheight, childheight);

Lineviews.add (Childview);
}

Childviewlist.add (lineviews);
This.lineHeight.add (lineheight);

int left = Getpaddingleft ();
int top = Getpaddingtop ();

for (int i = 0; i < childviewlist.size (); i++) {
Lineviews = Childviewlist.get (i);
for (int j = 0; J < Lineviews.size (); j + +) {
View Childview = Lineviews.get (j);

Marginlayoutparams params = (marginlayoutparams) childview.getlayoutparams ();

int LC = left + Params.leftmargin;
int TC = top + params.topmargin;
int RC = LC + Childview.getmeasuredwidth ();
int BC = TC + childview.getmeasuredheight ();

Childview.layout (LC,TC,RC,BC);

Left + = Params.leftmargin + childview.getmeasuredwidth () + Params.rightmargin;
}

left = Getpaddingleft ();
Top + = This.lineHeight.get (i);
}
}


Code:
Baidu Network disk: Http://pan.baidu.com/s/1hqH1kFU

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Custom Streaming layout

Related Article

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.