public class

ChainReference

extends HelperReference

 java.lang.Object

androidx.constraintlayout.core.state.ConstraintReference

androidx.constraintlayout.core.state.HelperReference

↳androidx.constraintlayout.core.state.helpers.ChainReference

Subclasses:

HorizontalChainReference, VerticalChainReference

Gradle dependencies

compile group: 'androidx.constraintlayout', name: 'constraintlayout-core', version: '1.1.0-beta01'

  • groupId: androidx.constraintlayout
  • artifactId: constraintlayout-core
  • version: 1.1.0-beta01

Artifact androidx.constraintlayout:constraintlayout-core:1.1.0-beta01 it located at Google repository (https://maven.google.com/)

Overview

HelperReference for Chains. Elements should be added with ChainReference.addChainElement(String, float, float, float)

Summary

Fields
protected floatmBias

protected java.util.HashMap<java.lang.String, java.lang.Float>mMapPostMargin

protected java.util.HashMap<java.lang.String, java.lang.Float>mMapPreMargin

protected java.util.HashMap<java.lang.String, java.lang.Float>mMapWeights

protected State.ChainmStyle

from HelperReferencemHelperState, mReferences
from ConstraintReferencemBottomToBottom, mBottomToTop, mEndToEnd, mEndToStart, mHorizontalBias, mLeftToLeft, mLeftToRight, mMarginBottom, mMarginBottomGone, mMarginEnd, mMarginEndGone, mMarginLeft, mMarginLeftGone, mMarginRight, mMarginRightGone, mMarginStart, mMarginStartGone, mMarginTop, mMarginTopGone, mRightToLeft, mRightToRight, mStartToEnd, mStartToStart, mTopToBottom, mTopToTop, mVerticalBias
Constructors
publicChainReference(State state, State.Helper type)

Methods
public voidaddChainElement(java.lang.Object id, float weight, float preMargin, float postMargin, float preGoneMargin, float postGoneMargin)

Adds the element by the given id to the Chain.

public voidaddChainElement(java.lang.String id, float weight, float preMargin, float postMargin)

Adds the element by the given id to the Chain.

public ChainReferencebias(float bias)

public floatgetBias()

protected floatgetPostMargin(java.lang.String id)

protected floatgetPreMargin(java.lang.String id)

public State.ChaingetStyle()

protected floatgetWeight(java.lang.String id)

public ChainReferencestyle(State.Chain style)

Sets the .

from HelperReferenceadd, apply, applyBase, getConstraintWidget, getHelperWidget, getType, setHelperWidget
from ConstraintReferenceaddCustomColor, addCustomFloat, alpha, applyWidgetConstraints, baseline, baselineToBaseline, baselineToBottom, baselineToTop, bottom, bottomToBottom, bottomToTop, centerHorizontally, centerVertically, circularConstraint, clear, clearAll, clearHorizontal, clearVertical, createConstraintWidget, end, endToEnd, endToStart, getAlpha, getFacade, getHeight, getHorizontalChainStyle, getHorizontalChainWeight, getKey, getPivotX, getPivotY, getRotationX, getRotationY, getRotationZ, getScaleX, getScaleY, getTag, getTranslationX, getTranslationY, getTranslationZ, getVerticalChainStyle, getVerticalChainWeight, getView, getWidth, height, horizontalBias, left, leftToLeft, leftToRight, margin, margin, marginGone, marginGone, pivotX, pivotY, right, rightToLeft, rightToRight, rotationX, rotationY, rotationZ, scaleX, scaleY, setConstraintWidget, setFacade, setHeight, setHorizontalChainStyle, setHorizontalChainWeight, setKey, setTag, setVerticalChainStyle, setVerticalChainWeight, setView, setWidth, start, startToEnd, startToStart, top, topToBottom, topToTop, translationX, translationY, translationZ, validate, verticalBias, visibility, width
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

protected float mBias

protected java.util.HashMap<java.lang.String, java.lang.Float> mMapWeights

Deprecated: Unintended visibility, use ChainReference.getWeight(String) instead

protected java.util.HashMap<java.lang.String, java.lang.Float> mMapPreMargin

Deprecated: Unintended visibility, use ChainReference.getPreMargin(String) instead

protected java.util.HashMap<java.lang.String, java.lang.Float> mMapPostMargin

Deprecated: Unintended visibility, use ChainReference.getPostMargin(String) instead

protected State.Chain mStyle

Constructors

public ChainReference(State state, State.Helper type)

Methods

public State.Chain getStyle()

public ChainReference style(State.Chain style)

Sets the .

Parameters:

style: Defines the way the chain will lay out its elements

Returns:

This same instance

public void addChainElement(java.lang.String id, float weight, float preMargin, float postMargin)

Adds the element by the given id to the Chain. The order in which the elements are added is important. It will represent the element's position in the Chain.

Parameters:

id: Id of the element to add
weight: Weight used to distribute remaining space to each element
preMargin: Additional space in pixels between the added element and the previous one (if any)
postMargin: Additional space in pixels between the added element and the next one (if any)

public void addChainElement(java.lang.Object id, float weight, float preMargin, float postMargin, float preGoneMargin, float postGoneMargin)

Adds the element by the given id to the Chain. The object's toString result will be used to map the given margins and weight to it, so it must stable and comparable. The order in which the elements are added is important. It will represent the element's position in the Chain.

Parameters:

id: Id of the element to add
weight: Weight used to distribute remaining space to each element
preMargin: Additional space in pixels between the added element and the previous one (if any)
postMargin: Additional space in pixels between the added element and the next one (if any)
preGoneMargin: Additional space in pixels between the added element and the previous one (if any) when the previous element has Gone visibility
postGoneMargin: Additional space in pixels between the added element and the next one (if any) when the next element has Gone visibility

protected float getWeight(java.lang.String id)

protected float getPostMargin(java.lang.String id)

protected float getPreMargin(java.lang.String id)

public float getBias()

public ChainReference bias(float bias)

Source

/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.constraintlayout.core.state.helpers;

import static androidx.constraintlayout.core.widgets.ConstraintWidget.UNKNOWN;

import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;
import androidx.constraintlayout.core.state.HelperReference;
import androidx.constraintlayout.core.state.State;

import java.util.HashMap;

/**
 * {@link HelperReference} for Chains.
 *
 * Elements should be added with {@link ChainReference#addChainElement}
 */
public class ChainReference extends HelperReference {

    protected float mBias = 0.5f;

    /**
     * @deprecated Unintended visibility, use {@link #getWeight(String)} instead
     */
    @Deprecated // TODO(b/253515185): Change to private visibility once we change major version
    protected @NonNull HashMap<String, Float> mMapWeights = new HashMap<>();

    /**
     * @deprecated Unintended visibility, use {@link #getPreMargin(String)} instead
     */
    @Deprecated // TODO(b/253515185): Change to private visibility once we change major version
    protected @NonNull HashMap<String, Float> mMapPreMargin = new HashMap<>();

    /**
     * @deprecated Unintended visibility, use {@link #getPostMargin(String)} instead
     */
    @Deprecated // TODO(b/253515185): Change to private visibility once we change major version
    protected @NonNull HashMap<String, Float> mMapPostMargin = new HashMap<>();

    private HashMap<String, Float> mMapPreGoneMargin;
    private HashMap<String, Float> mMapPostGoneMargin;

    protected @NonNull State.Chain mStyle = State.Chain.SPREAD;

    public ChainReference(@NonNull State state, @NonNull State.Helper type) {
        super(state, type);
    }

    public @NonNull State.Chain getStyle() {
        return State.Chain.SPREAD;
    }

    /**
     * Sets the {@link State.Chain style}.
     *
     * @param style Defines the way the chain will lay out its elements
     * @return This same instance
     */
    @NonNull
    public ChainReference style(@NonNull State.Chain style) {
        mStyle = style;
        return this;
    }

    /**
     * Adds the element by the given id to the Chain.
     *
     * The order in which the elements are added is important. It will represent the element's
     * position in the Chain.
     *
     * @param id         Id of the element to add
     * @param weight     Weight used to distribute remaining space to each element
     * @param preMargin  Additional space in pixels between the added element and the previous one
     *                   (if any)
     * @param postMargin Additional space in pixels between the added element and the next one (if
     *                   any)
     */
    public void addChainElement(@NonNull String id,
            float weight,
            float preMargin,
            float postMargin) {
        addChainElement(id, weight, preMargin, postMargin, 0, 0);
    }

    /**
     * Adds the element by the given id to the Chain.
     *
     * The object's {@link Object#toString()} result will be used to map the given margins and
     * weight to it, so it must stable and comparable.
     *
     * The order in which the elements are added is important. It will represent the element's
     * position in the Chain.
     *
     * @param id             Id of the element to add
     * @param weight         Weight used to distribute remaining space to each element
     * @param preMargin      Additional space in pixels between the added element and the
     *                       previous one
     *                       (if any)
     * @param postMargin     Additional space in pixels between the added element and the next
     *                       one (if
     *                       any)
     * @param preGoneMargin  Additional space in pixels between the added element and the previous
     *                       one (if any) when the previous element has Gone visibility
     * @param postGoneMargin Additional space in pixels between the added element and the next
     *                       one (if any) when the next element has Gone visibility
     */
    @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    public void addChainElement(@NonNull Object id,
            float weight,
            float preMargin,
            float postMargin,
            float preGoneMargin,
            float postGoneMargin) {
        super.add(id); // Add element id as is, it's expected to return the same given instance
        String idString = id.toString();
        if (!Float.isNaN(weight)) {
            mMapWeights.put(idString, weight);
        }
        if (!Float.isNaN(preMargin)) {
            mMapPreMargin.put(idString, preMargin);
        }
        if (!Float.isNaN(postMargin)) {
            mMapPostMargin.put(idString, postMargin);
        }
        if (!Float.isNaN(preGoneMargin)) {
            if (mMapPreGoneMargin == null) {
                mMapPreGoneMargin = new HashMap<>();
            }
            mMapPreGoneMargin.put(idString, preGoneMargin);
        }
        if (!Float.isNaN(postGoneMargin)) {
            if (mMapPostGoneMargin == null) {
                mMapPostGoneMargin = new HashMap<>();
            }
            mMapPostGoneMargin.put(idString, postGoneMargin);
        }
    }

    protected float getWeight(@NonNull String id) {
        if (mMapWeights.containsKey(id)) {
            return mMapWeights.get(id);
        }
        return UNKNOWN;
    }

    protected float getPostMargin(@NonNull String id) {
        if (mMapPostMargin.containsKey(id)) {
            return mMapPostMargin.get(id);
        }
        return 0;
    }

    protected float getPreMargin(@NonNull String id) {
        if (mMapPreMargin.containsKey(id)) {
            return mMapPreMargin.get(id);
        }
        return 0;
    }

    float getPostGoneMargin(@NonNull String id) {
        if (mMapPostGoneMargin != null && mMapPostGoneMargin.containsKey(id)) {
            return mMapPostGoneMargin.get(id);
        }
        return 0;
    }

    float getPreGoneMargin(@NonNull String id) {
        if (mMapPreGoneMargin != null && mMapPreGoneMargin.containsKey(id)) {
            return mMapPreGoneMargin.get(id);
        }
        return 0;
    }

    public float getBias() {
        return mBias;
    }

    // @TODO: add description
    @NonNull
    @Override
    public ChainReference bias(float bias) {
        mBias = bias;
        return this;
    }
}