android - How to make a grid layout of CardViews with variable height? -
i have multiple cardview
's defined in xml as:
<android.support.v7.widget.cardview android:layout_width="wrap_content" android:layout_height="wrap_content" card_view:cardpreventcorneroverlap="false" card_view:cardcornerradius="4dp"> <linearlayout android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content"> <textview android:textsize="@dimen/abc_text_size_title_material" android:text="lorem ipsum dolor sit amet, consectetur adipiscing elit" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout> </android.support.v7.widget.cardview>
for phones, have wrapped multiple cardview
's in vertical linearlayout
:
<linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
for tablets, use grid layout instead, similar design used in material design specs, see image here.
what's best viewgroup
use make grid layout? cards of variable height, , have static content.
gridlayout
can't used, because cards of variable height.
i had faced same problem time back. yes, can not use gridlayout
cards of variable size. looking staggeredgridview allows items have variable sizes.
you can do either of following :-
1) can use this or this library. simple use. have used both , can assure work perfectly.
2) can try recyclerview.staggeredgridlayoutmanager google.
a sample code somethings this,
recyclerview recyclerview = (recyclerview) findviewbyid(r.id.recycler_view); recyclerview.setlayoutmanager(new staggeredgridlayoutmanager(2, staggeredgridlayoutmanager.vertical)); recyclerview.setadapter(adapter);
hope helps you.
Comments
Post a Comment