google / flexbox-layout

Flexbox for Android
Apache License 2.0
18.26k stars 1.8k forks source link

Support RecyclerView of which height is wrap_content #349

Open ywwynm opened 7 years ago

ywwynm commented 7 years ago

If the RecyclerView's height is wrap_content, only one line of items will be shown and we should scroll the RecyclerView to see the second line(FlexDirection is ROW). It was said that if FlexWrap is set to WRAP, multiple lines will be shown but the fact is not like that. Please support this, thanks.

thagikura commented 7 years ago

I thought it's supported. Could you share your XML configuration for the RecyclerView and the settings for the FleboxLayoutManager?

ywwynm commented 7 years ago

@thagikura My layout(for a BottomSheet and is included in main layout) is:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  app:behavior_peekHeight="56dp"
  app:layout_behavior="@string/bottom_sheet_behavior">

  <android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</RelativeLayout>

And item's layout:

<FrameLayout
  android:layout_width="wrap_content"
  android:layout_height="42dp">

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

</FrameLayout>

And settings for FlexboxLayoutManager(in kotlin and actually settings are default ones):

val flm = FlexboxLayoutManager(this)
rv.layoutManager = flm
ywwynm commented 7 years ago

@thagikura Actually the bug is: some last line won't be shown under some circumstances(I'm still working on finding what these conditions are), not "only shows first line".

thagikura commented 7 years ago

Thanks for the info. I guess the height may be constrained by another component. When the last line isn't shown, is the bottom sheet fully shown or is it in its peek position?

ywwynm commented 7 years ago

@thagikura I think I find the reason causes the bug: if the item of RecyclerView is a TextView having a compound drawable, then the display will not be correct. However, if I replace it with an ImageView and a TextView inside a LinearLayout to achieve same effect, then the RecyclerView will show all items. Please import attached demo project below and take a look at the bug. Thank you. FlexboxLayoutManagerBug.zip

thagikura commented 7 years ago

Thanks for creating the test project. Confirmed the weird behavior, the issue seems to be caused if any compound drawables are included in the item in the RecyclerView.

In the initial call of the onLayoutChidlren, the width of the TextViews haivng a compound drawable seems to be calculated as if they don't have the compound drawable. As a result, the number of flex lines is shorter than expected.

Let me mark this as a bug.

ywwynm commented 7 years ago

@thagikura Thanks. I will wait for the fix but currently the LinearLayout stuff is adoptable.

Bringoff commented 6 years ago

Looks like I have the same behavior when TextView in ListItem has custom background (like this in my case:)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/color_tag_background" />
    <stroke
        android:width="1dp"
        android:color="@color/color_tag_border" />
    <corners android:radius="12dp" />
</shape>
MedveDomg commented 6 years ago

if we have three items at one line with linear layout manager then change for flex layout manager, only two items will be shown on and third one will be on next line

matheusfinatti commented 5 years ago

Also happens when using android.support.design.chip.Chip. In some instances the FlexboxLayoutManager is able to wrap the list of items and create a new line, however in some conditions it doesn't and the item is shown after scrolling the recyclerView. Does anyone know a workaround for this?

soldierinwhite commented 5 years ago

I am seeing the same bug with items that have a constraintlayout parent, with textview and imagebutton as children.