RecyclerView.md 4.5 KB
Newer Older
1 2 3 4
# RecyclerView integration

With the latest alpha version of the release, Flexbox can now be used inside the `RecyclerView`
as a `LayoutManager` (`FlexboxLayoutManager`).
5
That means now you can use Flexbox with a large number of items in a scrollable container!
6 7 8 9 10

![FlexboxLayoutManager in action](/assets/flexbox-layoutmanager.gif)


## Supported attributes / features comparison
11
Due to some characteristics of the RecyclerView, some Flexbox attributes are not available/not implemented
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
to the `FlexboxLayoutManager`.
Here is a quick overview of the attributes/features comparison between the two containers.

|Attribute / Feature|FlexboxLayout| FlexboxLayoutManager (RecyclerView)|
| ------- |:-----------:|:----------------------------------:|
|flexDirection|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|flexWrap|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png) (except `wrap_reverse`)|
|justifyContent|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|alignItems|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|alignContent|![Check](/assets/pngs/check_green_small.png)| - |
|layout_order|![Check](/assets/pngs/check_green_small.png)| - |
|layout_flexGrow|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|layout_flexShrink|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|layout_alignSelf|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|layout_flexBasisPercent|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|layout_(min/max)Width|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|layout_(min/max)Height|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|layout_wrapBefore|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|Divider|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|View recycling| - |![Check](/assets/pngs/check_green_small.png)|
|Scrolling| *1 |![Check](/assets/pngs/check_green_small.png)|

*1 Partially possible by wrapping it with `ScrollView`. But it isn't likely to work with large set
   of views inside the layout. Because it doesn't consider view recycling.

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
## Setting Flexbox attributes
You can set the attributes through Java code instead of settings those from XML for the `FlexboxLayoutManager`.
For example when you want change the `flexDirection` and `justifyContent`:

```java
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager();
layoutManager.setFlexDirection(FlexDirection.COLUMN);
layoutManager.setJustifyContent(JustifyContent.FLEX_END);
```

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) {
G
Gaëtan Muller 已提交
53
    FlexboxLayoutManager.LayoutParams flexboxLp = (FlexboxLayoutManager.LayoutParams) lp;
54 55 56 57
    flexboxLp.setFlexGrow(1.0f);
    flexboxLp.setAlignSelf(AlignSelf.FLEX_END);
}
```
58

T
Takeshi Hagikura 已提交
59
## Backward-incompatible changes from the 0.2.x versions
60
`FlexboxLayout` can still be used as the same way, but there are some backward-incompatible
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
changes introduced.

* Now Flexbox specific constants are now defined in each individual class such as:
  * `FlexboxLayout.FLEX_DIRECTION_ROW` -> `FlexDirection.ROW`
  * `FlexboxLayout.FLEX_WRAP_WRAP` -> `FlexWrap.WRAP`
  * `FlexboxLayout.JUSTIFY_CONTENT_FLEX_START` -> `JustifyContent.FLEX_START`
  * `FlexboxLayout.ALIGN_ITEMS_FLEX_START` -> `AlignItems.FLEX_START`
  * `FlexboxLayout.ALIGN_CONTENT_FLEX_START` -> `AlignContent.FLEX_START`

  including other values (such as FLEX_END, STRETCH) are now moved to each individual class.

## Sample code
The code for the new `FlexboxLayoutManager` hasn't merged to the master branch yet, since
it's not as stable as the existing `FlexboxLayout`.
But you can still reference some sample code using the `FlexboxLayoutManager` inside the
`RecyclerView` in the [dev_recyclerview](https://github.com/google/flexbox-layout/tree/dev_recyclerview) branch
such as:
  -  [Playground demo app](https://github.com/google/flexbox-layout/tree/dev_recyclerview/demo-playground)
  -  [Cat Gallery demo app](https://github.com/google/flexbox-layout/tree/dev_recyclerview/demo-cat-gallery)