Android Open Source - ExpertAndroid Flow Layout






From Project

Back to project page ExpertAndroid.

License

The source code is released under:

MIT License

If you think the Android project ExpertAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.iuriio.demos.expertandroid.ch3flowlayout;
/*from w w w  .  j a  v a2 s  .  c  o m*/
import android.content.Context;
import android.content.res.TypedArray;
import android.text.Layout;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class FlowLayout extends ViewGroup {
    private int hspace = 10;
    private int vspace = 10;

    public FlowLayout(Context context) {
        super(context);
        this.initialize(context);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.FlowLayout, defStyle, 0);
        this.hspace = t.getDimensionPixelSize(R.styleable.FlowLayout_hspace, this.hspace);
        this.vspace = t.getDimensionPixelSize(R.styleable.FlowLayout_vspace, this.vspace);
        t.recycle();

        this.initialize(context);
    }

    private void initialize(Context context) {
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int childCount = this.getChildCount();
        for (int index = 0; index < childCount; index++) {
            final View child = this.getChildAt(index);
            LayoutParams lp = (LayoutParams) child.getLayoutParams();
            child.layout(lp.x, lp.y, lp.x + child.getMeasuredWidth(), lp.y + child.getMeasuredHeight());
        }
    }

    //This is very basic
    //doesn't take into account padding
    //You can easily modify it to account for padding
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int realWidth = MeasureSpec.getSize(widthMeasureSpec);
        int realHeight = MeasureSpec.getSize(heightMeasureSpec);

        int currentHeight = 0;
        int currentWidth = 0;
        int currentChildHeight = 0;
        int currentChildWidth = 0;

        int numOfChildren = this.getChildCount();
        for (int index = 0; index < numOfChildren; index++) {
            View child = this.getChildAt(index);
            this.measureChild(child, widthMeasureSpec, heightMeasureSpec);
            int childMeasuredWidth = child.getMeasuredWidth();
            int childMeasuredHeight = child.getMeasuredHeight();

            if (currentChildWidth + childMeasuredWidth > realWidth) {
                // new line: max of current width and current width position
                // when multiple lines are in play w could be maxed out
                // or in uneven sizes is the max of the right side lines
                // all lines don't have to have the same width
                // some may be larger than others
                currentWidth = Math.max(currentWidth, currentChildWidth);
                //reposition the point on the next line
                currentChildWidth = 0; //start of the line
                currentChildHeight = currentChildHeight + childMeasuredHeight; //add view height to the current height
            }

            int nextChildWidth = currentChildWidth + childMeasuredWidth;
            int nextChildHeight = currentChildHeight;

            //latest height: current point + height of the view
            //however if the previous height is larger use that one
            currentHeight = Math.max(currentHeight, currentChildHeight + childMeasuredHeight);

            // Save the current coords for the view in its layout
            LayoutParams lp = (LayoutParams) child.getLayoutParams();
            lp.x = currentChildWidth;
            lp.y = currentChildHeight;

            currentChildWidth = nextChildWidth;
            currentChildHeight = nextChildHeight;
        }

        currentWidth = Math.max(currentChildWidth, currentWidth);
        this.setMeasuredDimension(
                resolveSize(currentWidth, widthMeasureSpec),
                resolveSize(currentHeight, heightMeasureSpec));
    }

    /**
     * Returns a new set of layout parameters based on the supplied attributes set.
     *
     * @param attrs the attributes to build the layout parameters from
     * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
     * of its descendants
     */
    @Override
    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new FlowLayout.LayoutParams(this.getContext(), attrs);
    }

    /**
     * Returns a set of default layout parameters. These parameters are requested
     * when the View passed to {@link #addView(android.view.View)} has no layout parameters
     * already set. If null is returned, an exception is thrown from addView.
     *
     * @return a set of default layout parameters or null
     */
    @Override
    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }

    /**
     * Returns a safe set of layout parameters based on the supplied layout params.
     * When a ViewGroup is passed a View whose layout params do not pass the test of
     * {@link #checkLayoutParams(android.view.ViewGroup.LayoutParams)}, this method
     * is invoked. This method should return a new set of layout params suitable for
     * this ViewGroup, possibly by copying the appropriate attributes from the
     * specified set of layout params.
     *
     * @param p The layout parameters to convert into a suitable set of layout parameters
     *          for this ViewGroup.
     * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
     * of its descendants
     */
    @Override
    protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
        return new LayoutParams(p);
    }

    /**
     * {@inheritDoc}
     *
     * @param p
     */
    @Override
    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
        return p instanceof LayoutParams;
    }

    public static class LayoutParams extends MarginLayoutParams {
        public int spacing = -1;
        public int x = 0;
        public int y = 0;

        /**
         * Creates a new set of layout parameters. The values are extracted from
         * the supplied attributes set and context.
         *
         * @param c     the application environment
         * @param attrs the set of attributes from which to extract the layout
         */
        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);

            TypedArray t = c.obtainStyledAttributes(attrs, R.styleable.FlowLayout_Layout);
            this.spacing = t.getDimensionPixelSize(R.styleable.FlowLayout_Layout_layout_space, 0);
            t.recycle();
        }

        /**
         * {@inheritDoc}
         *
         * @param width
         * @param height
         */
        public LayoutParams(int width, int height) {
            super(width, height);
            this.spacing = 0;
        }

        /**
         * {@inheritDoc}
         *
         * @param source
         */
        public LayoutParams(ViewGroup.LayoutParams source) {
            super(source);
        }

        /**
         * Copy constructor. Clones the width, height and margin values of the source.
         *
         * @param source The layout params to copy from.
         */
        public LayoutParams(MarginLayoutParams source) {
            super(source);
        }
    }
}




Java Source Code List

com.androidbook.notebad.NoteEditor.java
com.androidbook.notebad.NotePadProvider.java
com.androidbook.notebad.NotePad.java
com.androidbook.notebad.NotesList.java
com.androidbook.notebad.TitleEditor.java
com.androidbook.parse.BaseActivity.java
com.androidbook.parse.BaseListActivity.java
com.androidbook.parse.CreateAMeaningActivity.java
com.androidbook.parse.CreateAWordActivity.java
com.androidbook.parse.Field.java
com.androidbook.parse.FormActivity.java
com.androidbook.parse.IReportBack.java
com.androidbook.parse.IValidator.java
com.androidbook.parse.IValueValidator.java
com.androidbook.parse.LoginActivity.java
com.androidbook.parse.ParseApplication.java
com.androidbook.parse.ParseObjectWrapperOld1.java
com.androidbook.parse.ParseObjectWrapper.java
com.androidbook.parse.ParseStarterProjectActivity.java
com.androidbook.parse.PasswordFieldRule.java
com.androidbook.parse.PasswordResetActivity.java
com.androidbook.parse.PasswordResetSuccessActivity.java
com.androidbook.parse.SignupActivity.java
com.androidbook.parse.SignupSuccessActivity.java
com.androidbook.parse.StringUtils.java
com.androidbook.parse.UserListActivity.java
com.androidbook.parse.WelcomeActivity.java
com.androidbook.parse.WordListActivity.java
com.androidbook.parse.WordListAdapter.java
com.androidbook.parse.WordMeaningListAdapter.java
com.androidbook.parse.WordMeaning.java
com.androidbook.parse.WordMeaningsListActivity.java
com.androidbook.parse.Word.java
com.iuriio.demos.expertandroid.ch10search.MainActivity.java
com.iuriio.demos.expertandroid.ch10search.SearchActivity.java
com.iuriio.demos.expertandroid.ch11searchprovider.MainActivity.java
com.iuriio.demos.expertandroid.ch11searchprovider.SearchActivity.java
com.iuriio.demos.expertandroid.ch11searchprovider.SimpleSuggestionProvider.java
com.iuriio.demos.expertandroid.ch11searchprovider.SuggestUrlProvider.java
com.iuriio.demos.expertandroid.ch13parsesimple.BaseActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.LoginActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.MainActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.ParseApp.java
com.iuriio.demos.expertandroid.ch13parsesimple.ParseObjectWrapper.java
com.iuriio.demos.expertandroid.ch13parsesimple.ParseStarterProjectActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.PasswordResetSuccessActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.ResetPasswordActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.SignupActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.StringUtils.java
com.iuriio.demos.expertandroid.ch13parsesimple.WordListActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.Word.java
com.iuriio.demos.expertandroid.ch1circleview.AbstractBaseView.java
com.iuriio.demos.expertandroid.ch1circleview.CircleView.java
com.iuriio.demos.expertandroid.ch1circleview.MainActivity.java
com.iuriio.demos.expertandroid.ch2durationcontrol.DatePickerFragment.java
com.iuriio.demos.expertandroid.ch2durationcontrol.DurationControl.java
com.iuriio.demos.expertandroid.ch2durationcontrol.MainActivity.java
com.iuriio.demos.expertandroid.ch3flowlayout.FlowLayout.java
com.iuriio.demos.expertandroid.ch3flowlayout.MainActivity.java
com.iuriio.demos.expertandroid.ch4gsonserialization.ChildObject.java
com.iuriio.demos.expertandroid.ch4gsonserialization.MainActivity.java
com.iuriio.demos.expertandroid.ch4gsonserialization.MainObject.java
com.iuriio.demos.expertandroid.ch6forms.BaseActivity.java
com.iuriio.demos.expertandroid.ch6forms.Field.java
com.iuriio.demos.expertandroid.ch6forms.FormActivity.java
com.iuriio.demos.expertandroid.ch6forms.IValidator.java
com.iuriio.demos.expertandroid.ch6forms.IValueValidator.java
com.iuriio.demos.expertandroid.ch6forms.MainActivity.java
com.iuriio.demos.expertandroid.ch6forms.PasswordFieldRule.java
com.iuriio.demos.expertandroid.ch6forms.StringUtils.java
com.iuriio.demos.expertandroid.ch6forms.WelcomeActivity.java
com.iuriio.demos.expertandroid.ch9openglexperiments.AbstractRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.AnimatedSimpleTriangleRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.ES20AbstractRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.ES20ControlledAnimatedTexturedCubeRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.ES20SimpleTriangleRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.ES20SingleTextureAbstractRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.FrustumDimensions.java
com.iuriio.demos.expertandroid.ch9openglexperiments.MainActivity.java
com.iuriio.demos.expertandroid.ch9openglexperiments.MyApplication.java
com.iuriio.demos.expertandroid.ch9openglexperiments.OpenGLES10Activity.java
com.iuriio.demos.expertandroid.ch9openglexperiments.OpenGLES20Activity.java
com.iuriio.demos.expertandroid.ch9openglexperiments.PolygonRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.RegularPolygon.java
com.iuriio.demos.expertandroid.ch9openglexperiments.Shape.java
com.iuriio.demos.expertandroid.ch9openglexperiments.SimpleTriangleRenderer.java
converters.FieldTransporter.java
converters.IFieldTransport.java
converters.IntegerFieldTransport.java
converters.ParseObjectEssentials.java
converters.StringFieldTransport.java
converters.User.java
converters.ValueField.java