Android simple way to implement custom streaming layouts _android

Source: Internet
Author: User
Tags sqlite database jcenter

The example in this article describes how Android simply implements a custom streaming layout. Share to everyone for your reference, specific as follows:

First look at a strike. HD-Product Details-Select the UI for the Product properties page

The product has many sizes, and the view size required for each size is different (mainly width), so the number of rows required to show all sizes and the number of rows per row is not determined until the data is pulled from the server, so the GridView or ListView cannot be used directly.

What if you use LinearLayout?

A linearlayout can only display a row, if you want to show more than one line, then each row will be a new linearlayout out, but also have to calculate how each linearlayout can accommodate the size of the 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 code, we call it flowlayout.

1. First create a new FlowLayout class, inherit from ViewGroup
2. FlowLayout height calculated in Onmeasure according to the views of the child
3. Layout of the views of the child in OnLayout (layout)

The following lists only the core pieces of code, the complete code has been placed on the GitHub-androidflowlayout, welcome to fork.

Calculate the height of flowlayout in Onmeasure

Iterate through all the child View for (int i = 0, ChildCount = Getchildcount (); i < ChildCount; ++i) {View Childview = Getchildat (i);
  Measure view, and get its width and height layoutparams childlayoutparams = childview.getlayoutparams ();
      Childview.measure (Getchildmeasurespec (widthmeasurespec, Paddingleft + paddingright, childLayoutParams.width),
  Getchildmeasurespec (Heightmeasurespec, Paddingtop + paddingbottom, childlayoutparams.height));
  int childwidth = Childview.getmeasuredwidth ();
  int childheight = Childview.getmeasuredheight ();
  Calculates the height of the current row (the tallest of all child view in the current row) Lineheight = Math.max (Childheight, lineheight); Put the current child view to the right of the previous child view, and if it doesn't fit, the newline if (childleft + childwidth + paddingright > mywidth) {childleft = P
    Addingleft;
    Childtop + + mverticalspacing + lineheight;
  Lineheight = Childheight;
  else {Childleft + = childwidth + mhorizontalspacing;
} int wantedheight = childtop + lineheight + paddingbottom; The height setmeasureddimension needed to compute FlowLayout (MYWIDTH, Resolvesize (Wantedheight, Heightmeasurespec));

 

To lay out the child views in onlayout

The code is very similar to onmeasure, simply by placing it at the specified location based on the width and height of the child view.

for (int i = 0, ChildCount = Getchildcount (); i < ChildCount ++i) {
  View Childview = Getchildat (i);
  if (childview.getvisibility () = = View.gone) {
    continue;
  }
  int childwidth = Childview.getmeasuredwidth ();
  int childheight = Childview.getmeasuredheight ();
  Lineheight = Math.max (Childheight, lineheight);
  if (childleft + childwidth + paddingright > Mywidth) {
    childleft = paddingleft;
    Childtop + + mverticalspacing + lineheight;
    Lineheight = childheight;
  }
  Key code
  childview.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 has been sent to the Bintray, the use of a very simple way, only to the project in the corresponding build.gradle to add a dependency.

Compile ' Com.liangfeizc:flowlayout:1.0.0@aar '

Upload the AAR to Jcenter.

The specific procedure can refer to the publishing Gradle Android library to Jcenter
Package scripts can refer to Flowlayout/build.gradle

More interested readers of Android-related content can view this site: "Android Layout Layout Skills Summary", "Android View tips Summary", "Android operation XML Data Skills summary", " Activity Operation Skills Summary of Android programming, "Android Resource Operation skills Summary", "Android File Operation tips Summary", "Android operation SQLite Database Skills Summary", "Android operation JSON format data Skills Summary" , "Android Database Operation skills Summary", "Android programming SD Card Operation method Summary", "Android Development introduction and Advanced Course" and "Android Control usage Summary"

I hope this article will help you with the Android program.

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.