提交 b422f037 编写于 作者: T Takeshi Hagikura

Add the common methods for the FlexContainer.

Adds the methods that FlexboxLayout and FlexboxLayoutManager have in common
in calculating the flexbox layout.

Change-Id: Ib0e0159cf0ea2cdc4f9c38020f4b34ec5eb2e537
上级 61e8cd1c
......@@ -18,11 +18,13 @@ package com.google.android.flexbox;
import android.view.View;
import java.util.List;
/**
* An interface that has the common behavior as the flex container such as {@link FlexboxLayout}
* and {@link FlexboxLayoutManager}.
*/
interface FlexContainer {
public interface FlexContainer {
/**
* @return the number of child count contained in the flex container.
......@@ -36,4 +38,98 @@ interface FlexContainer {
* @return the view at the index
*/
View getChildAt(int i);
/**
* Returns a View, which is reordered by taking the order attribute into account.
*
* @param i the index of the view
* @return the reordered view, which order attribute is taken into account.
* If the index is negative or out of bounds of the number of contained views,
* returns {@code null}.
* @see FlexItem#getOrder()
*/
View getReorderedChildAt(int i);
/**
* @return the flex direction attribute of the flex container.
* @see FlexDirection
*/
@FlexDirection
int getFlexDirection();
/**
* Sets the given flex direction attribute to the flex container.
*
* @param flexDirection the flex direction value
* @see FlexDirection
*/
void setFlexDirection(@FlexDirection int flexDirection);
/**
* @return the flex wrap attribute of the flex container.
* @see FlexWrap
*/
@FlexWrap
int getFlexWrap();
/**
* Sets the given flex wrap attribute to the flex container.
*
* @param flexWrap the flex wrap value
* @see FlexWrap
*/
void setFlexWrap(@FlexWrap int flexWrap);
/**
* @return the justify content attribute of the flex container.
* @see JustifyContent
*/
@JustifyContent
int getJustifyContent();
/**
* Sets the given justify content attribute to the flex container.
*
* @param justifyContent the justify content value
* @see JustifyContent
*/
void setJustifyContent(@JustifyContent int justifyContent);
/**
* @return the align content attribute of the flex container.
* @see AlignContent
*/
@AlignContent
int getAlignContent();
/**
* Sets the given align content attribute to the flex container.
*
* @param alignContent the align content value
*/
void setAlignContent(@AlignContent int alignContent);
/**
* @return the align items attribute of the flex container.
* @see AlignItems
*/
@AlignItems
int getAlignItems();
/**
* Sets the given align items attribute to the flex container.
*
* @param alignItems the align items value
* @see AlignItems
*/
void setAlignItems(@AlignItems int alignItems);
/**
* @return the flex lines composing this flex container. The overridden method should return a
* copy of the original list excluding a dummy flex line (flex line that doesn't have any flex
* items in it but used for the alignment along the cross axis) so that any changes of the
* returned list are not reflected to the original list.
*/
List<FlexLine> getFlexLines();
}
......@@ -628,7 +628,8 @@ public class FlexboxLayout extends ViewGroup implements FlexContainer {
/**
* Determine the main size by expanding (shrinking if negative remaining free space is given)
* an individual child in each flex line if any children's mFlexGrow (or mFlexShrink if remaining
* an individual child in each flex line if any children's mFlexGrow (or mFlexShrink if
* remaining
* space is negative) properties are set to non-zero.
*
* @param flexDirection the value of the flex direction
......@@ -774,7 +775,8 @@ public class FlexboxLayout extends ViewGroup implements FlexContainer {
} else {
// The direction of the main axis is vertical
if (!mChildrenFrozen[childIndex]) {
float rawCalculatedHeight = child.getMeasuredHeight() + unitSpace * lp.mFlexGrow;
float rawCalculatedHeight = child.getMeasuredHeight()
+ unitSpace * lp.mFlexGrow;
if (i == flexLine.mItemCount - 1) {
rawCalculatedHeight += accumulatedRoundError;
accumulatedRoundError = 0;
......@@ -2117,10 +2119,12 @@ public class FlexboxLayout extends ViewGroup implements FlexContainer {
}
@FlexDirection
@Override
public int getFlexDirection() {
return mFlexDirection;
}
@Override
public void setFlexDirection(@FlexDirection int flexDirection) {
if (mFlexDirection != flexDirection) {
mFlexDirection = flexDirection;
......@@ -2129,10 +2133,12 @@ public class FlexboxLayout extends ViewGroup implements FlexContainer {
}
@FlexWrap
@Override
public int getFlexWrap() {
return mFlexWrap;
}
@Override
public void setFlexWrap(@FlexWrap int flexWrap) {
if (mFlexWrap != flexWrap) {
mFlexWrap = flexWrap;
......@@ -2141,10 +2147,12 @@ public class FlexboxLayout extends ViewGroup implements FlexContainer {
}
@JustifyContent
@Override
public int getJustifyContent() {
return mJustifyContent;
}
@Override
public void setJustifyContent(@JustifyContent int justifyContent) {
if (mJustifyContent != justifyContent) {
mJustifyContent = justifyContent;
......@@ -2153,10 +2161,12 @@ public class FlexboxLayout extends ViewGroup implements FlexContainer {
}
@AlignItems
@Override
public int getAlignItems() {
return mAlignItems;
}
@Override
public void setAlignItems(@AlignItems int alignItems) {
if (mAlignItems != alignItems) {
mAlignItems = alignItems;
......@@ -2165,10 +2175,12 @@ public class FlexboxLayout extends ViewGroup implements FlexContainer {
}
@AlignContent
@Override
public int getAlignContent() {
return mAlignContent;
}
@Override
public void setAlignContent(@AlignContent int alignContent) {
if (mAlignContent != alignContent) {
mAlignContent = alignContent;
......@@ -2182,6 +2194,7 @@ public class FlexboxLayout extends ViewGroup implements FlexContainer {
* but used for the alignment along the cross axis).
* Thus any changes of the returned list are not reflected to the original list.
*/
@Override
public List<FlexLine> getFlexLines() {
List<FlexLine> result = new ArrayList<>(mFlexLines.size());
for (FlexLine flexLine : mFlexLines) {
......@@ -2498,7 +2511,8 @@ public class FlexboxLayout extends ViewGroup implements FlexContainer {
mFlexBasisPercent = a
.getFraction(R.styleable.FlexboxLayout_Layout_layout_flexBasisPercent, 1, 1,
FLEX_BASIS_PERCENT_DEFAULT);
mMinWidth = a.getDimensionPixelSize(R.styleable.FlexboxLayout_Layout_layout_minWidth, 0);
mMinWidth = a
.getDimensionPixelSize(R.styleable.FlexboxLayout_Layout_layout_minWidth, 0);
mMinHeight = a
.getDimensionPixelSize(R.styleable.FlexboxLayout_Layout_layout_minHeight, 0);
mMaxWidth = a.getDimensionPixelSize(R.styleable.FlexboxLayout_Layout_layout_maxWidth,
......
......@@ -25,6 +25,9 @@ import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
/**
* LayoutManager for the {@link RecyclerView}. This class is intended to be used within a
* {@link RecyclerView} and offers the same capabilities of measure/layout its children
......@@ -61,6 +64,8 @@ public class FlexboxLayoutManager extends RecyclerView.LayoutManager implements
*/
private int mAlignContent;
private List<FlexLine> mFlexLines = new ArrayList<>();
private final FlexboxHelper mFlexboxHelper = new FlexboxHelper(this);
/**
......@@ -138,10 +143,12 @@ public class FlexboxLayoutManager extends RecyclerView.LayoutManager implements
}
@FlexDirection
@Override
public int getFlexDirection() {
return mFlexDirection;
}
@Override
public void setFlexDirection(@FlexDirection int flexDirection) {
if (mFlexDirection != flexDirection) {
mFlexDirection = flexDirection;
......@@ -149,11 +156,13 @@ public class FlexboxLayoutManager extends RecyclerView.LayoutManager implements
}
}
@Override
@FlexWrap
public int getFlexWrap() {
return mFlexWrap;
}
@Override
public void setFlexWrap(@FlexWrap int flexWrap) {
if (mFlexWrap != flexWrap) {
mFlexWrap = flexWrap;
......@@ -162,10 +171,12 @@ public class FlexboxLayoutManager extends RecyclerView.LayoutManager implements
}
@JustifyContent
@Override
public int getJustifyContent() {
return mJustifyContent;
}
@Override
public void setJustifyContent(@JustifyContent int justifyContent) {
if (mJustifyContent != justifyContent) {
mJustifyContent = justifyContent;
......@@ -174,10 +185,12 @@ public class FlexboxLayoutManager extends RecyclerView.LayoutManager implements
}
@AlignItems
@Override
public int getAlignItems() {
return mAlignItems;
}
@Override
public void setAlignItems(@AlignItems int alignItems) {
if (mAlignItems != alignItems) {
mAlignItems = alignItems;
......@@ -186,10 +199,12 @@ public class FlexboxLayoutManager extends RecyclerView.LayoutManager implements
}
@AlignContent
@Override
public int getAlignContent() {
return mAlignContent;
}
@Override
public void setAlignContent(@AlignContent int alignContent) {
if (mAlignContent != alignContent) {
mAlignContent = alignContent;
......@@ -197,6 +212,18 @@ public class FlexboxLayoutManager extends RecyclerView.LayoutManager implements
}
}
@Override
public List<FlexLine> getFlexLines() {
List<FlexLine> result = new ArrayList<>(mFlexLines.size());
for (FlexLine flexLine : mFlexLines) {
if (flexLine.getItemCount() == 0) {
continue;
}
result.add(flexLine);
}
return result;
}
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册