Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically calculating size of RecyclerView populated with StaggeredGridLayoutManager

I have a RecyclerView with StaggeredGridLayoutManager inside a ScrollView. As you can't set WRAP_CONTENT parameter for RecylerView (doesn't support it), I have to calculate the height of view, dynamically. (Adapter gets populated dynamically. Its an infinite scroll view). You can find similar behavior in Pinterest app. (Pictured below). I also wrote my layout view hierarchy.

<ScrollView>
  <LinearLayout>
    <some_layouts>
     .
     .
    </some_layouts>  
    <RecyclerView>
    </RecyclerView>  
  <LinearLayout>
<ScrollView>

How can I get same thing in my app? I dug through the classes api but found nothing. Can anyone help me on this?

enter image description here

like image 341
Alireza Farahani Avatar asked Nov 09 '22 11:11

Alireza Farahani


1 Answers

Although answer is too late and i was also finding the solution i came across the library in which size of the item layout is dynamically adjusted with few lines of code.

Just add

dependencies {

implementation 'com.google.android:flexbox:0.3.2'

}

Github Library Link

Use below code:-

FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(getContext());
                layoutManager.setFlexWrap(FlexWrap.WRAP);

recyclerView.setLayoutManager(layoutManager);

So you don't have calculate height dynamically.

like image 114
Karan sharma Avatar answered Nov 14 '22 21:11

Karan sharma