+0
日增长
18,272
stars
1,795
forks
0
watchers
292
prs
336
issues
25
releases
README
FlexboxLayout

FlexboxLayout is a library project which brings the similar capabilities of
CSS Flexible Box Layout Module to Android.
Installation
Add the following dependency to your build.gradle file:
dependencies {
implementation 'com.google.android.flexbox:flexbox:3.0.0'
}
Starting from 3.0.0, the groupId is changed to com.google.android.flexbox in preparation to uploading the artifacts to google maven.
You can still download the artifacts from jcenter for the past versions with the prior groupId (com.google.android), but migrating the library 3.0.0 is recommended.
Note that the default values for alignItems and alignContent for FlexboxLayout have been changed from stretch to flex_start starting from 2.0.0, it may break the existing apps.
Please make sure to set stretch explicitly if you want to apply the behavior of stretch.
Note that starting from 1.1.0, the library is expeced to use with AndroidX. Please migrate to AndroidX if you use 1.1.0 or above.
Please use 1.0.0 if you haven't migrated to AndroidX.
Usage
There are two ways of using Flexbox in your layout.
FlexboxLayout
The first one is FlexboxLayout that extends the ViewGroup like LinearLayout and RelativeLayout.
You can specify the attributes from a layout XML like:
xml
Or from code like:
java
FlexboxLayout flexboxLayout = (FlexboxLayout) findViewById(R.id.flexbox_layout);
flexboxLayout.setFlexDirection(FlexDirection.ROW);
View view = flexboxLayout.getChildAt(0);
FlexboxLayout.LayoutParams lp = (FlexboxLayout.LayoutParams) view.getLayoutParams();
lp.setOrder(-1);
lp.setFlexGrow(2);
view.setLayoutParams(lp);
FlexboxLayoutManager (within RecyclerView)
The second one is FlexboxLayoutManager that can be used within RecyclerView.
java
RecyclerView recyclerView = (RecyclerView) context.findViewById(R.id.recyclerview);
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context);
layoutManager.setFlexDirection(FlexDirection.COLUMN);
layoutManager.setJustifyContent(JustifyContent.FLEX_END);
recyclerView.setLayoutManager(layoutManager);
or for the attributes for the children of the FlexboxLayoutManager you can do like:
java
mImageView.setImageDrawable(drawable);
ViewGroup.LayoutParams lp = mImageView.getLayoutParams();
if (lp instanceof FlexboxLayoutManager.LayoutParams) {
FlexboxLayoutManager.LayoutParams flexboxLp = (FlexboxLayoutManager.LayoutParams) lp;
flexboxLp.setFlexGrow(1.0f);
flexboxLp.setAlignSelf(AlignSelf.FLEX_END);
}
The advantage of using FlexboxLayoutManager is that it recycles the views that go off the screen
for reuse for the views that are appearing as the user scrolls instead of inflating every individual view,
which consumes much less memory especially when the number of items contained in the Flexbox container is large.
!FlexboxLayoutManager in action
Supported attributes/features comparison
Due to some characteristics of ReTop issues
#420FlexboxLayoutManager did not display all items in NestedScrollView (item not displayed is hidden)48 comments · 2018-03-30#363ArrayIndexOutOfBoundsException in updateLayoutState()28 comments · 2017-10-02#440Flexbox showing a lot of items on the last line of maxLine26 comments · 2018-06-27#240RecyclerView crashing in some circumstances26 comments · 2017-03-29#449FlexboxLayoutManager with FlexboxItemDecoration cause item height incorrent11 comments · 2018-07-23#349Support RecyclerView of which height is wrap_content11 comments · 2017-08-18#336FlexboxItemDecoration not correct in FlexboxItemDecoration9 comments · 2017-07-25#640Update your Play Core Maven dependency to an Android 14 compatible version!7 comments · 2024-06-19
Latest release
3.0.0 3.0.0 2021-05-21Artifacts are now hosted on google maven Starting from 3.0.0 and moving forward, this library is hosted on google maven. In relation to the change of the hosted server, the groupId of this library was changed from com.google.android to com.google.android.flexbox. Old versions of libraries are still hosted on JCenter as read only mode.