Implementation of streaming layouts-1

Source: Internet
Author: User

Flow layout can be filled with line-by-row layout effect, for keyword search and popular display, you can dynamically add tags, it is very convenient and fast.

Source download (provided by teachers of MU-class network, thank you)

Then talk about the main arrangements:

First: Create classes, determine inheritance relationships, implement constructors, determine member functions;

The second chapter: the method of realizing the main function of FlowLayout (flow layout);

First: Create classes, determine inheritance relationships, implement constructors, determine member functions;

Second and later: implement each function, and explain the role of member variables;

As with the ListView implementation of the drop-down refresh, the file structure is analyzed first:

Two classes are included:

public class Mainactivity extends Activity;public class FlowLayout extends ViewGroup;

Mainactivity: Used to load the main layout and add the child view dynamically to the custom layout;

FlowLayout: Inherited from ViewGroup, used to implement streaming layouts; ViewGroup is a container for various UI components, inherited from view (official explanation: A viewgroup is a special view that can contain Other views (called children.) The view group is the base class for layouts and views containers.), common inherited from ViewGroup has our familiar ListView (ListView <-Abslist View <-Adapterview <-viewgroup);

In the activity:

Member variables:

Private string[] Mvals;private FlowLayout mflowlayout;

Mvals: Used to dynamically load the label as the text data source of the TextView;

Mflowlayout:flowlayout layout;

member functions:

public void InitData ()

InitData: Adds a label, writes the characters in the Mvals to the label, and loads the label into the FlowLayout layout;

Init method:

public void InitData ()    {        Layoutinflater-minflater = Layoutinflater.from (this);        for (int i = 0; i < mvals.length; i++)        {            TextView TV = (TextView) minflater.inflate (r.layout.tv,                    mflowlayou T, false);            Tv.settext (Mvals[i]);            Mflowlayout.addview (TV);        }     }

First call the Layoutinflater static member function from, get the main layout of the Layoutinflater, so that later set the main layout of the nested FlowLayout layout of the View object;

Then in the For loop, the main layout of the inflate function to get a new TextView object, the data source mvals in the data written to the TextView, the FlowLayout layout of the AddView method, the new TextView added to it;

In the FlowLayout class:

Member variables:

Private list<list<view>> mallviews;private list<integer> mlineheight;

Mallviews: Store all the view;

Mlineheight: The height of each row;

member functions:

Public FlowLayout (Context context);p Ublic FlowLayout (context context, AttributeSet attrs);p ublic flowlayout (context Context, AttributeSet attrs, int defstyle);p rotected void onmeasure (int widthmeasurespec, int heightmeasurespec);  protected void OnLayout (Boolean changed, int l, int t, int r, int b);p ublic layoutparams generatelayoutparams (attributeset ATTRS);

Parameters of each constructor, call timing and rewrite:
A parameter of: for the context; New a control, passing in the context object; Override, let him invoke two parameters of the construction method;
Two parameters: for context and attribute set, is called when writing a property of a control in a layout file and no custom property, overriding, allowing him to invoke the construction method of three arguments;
Three parameters: called for context, property set, and Defstyle, when writing the properties of a control in a layout file and using a custom property;
Onmerasure: Measure the view and its contents to determine the width and height of the view. This method is called in measure (int, int) and must be rewritten to accurately and effectively measure the contents of the view;

OnLayout: Called when a view is set to its child's size and position. Child view, including children, must override the OnLayout (boolean, int, int, int, int) methods, and invoke the respective layout (int, int, int, int) methods. The parameter changed indicates that the view has a new dimension or position; The parameter L represents the left position relative to the parent view, the parameter T represents the top position relative to the parent view, and the parameter r represents the right position relative to the parent view, and the parameter B represents the bottom position relative to the parent view.

Generatelayoutparams: Indicates the relationship between ViewGroup and Layoutparams, because the spacing is specified so that the direct return to the new Marginlayoutparams (GETCONTEXT,ATTRS);








Implementation of streaming layouts-1

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.