android - center items horizontally in RecyclerView(using GridLayoutManager) -
i'm trying center items horizontally each row in recyclerview
i tried many things: relativelayout parent, use layout_centerinparent, use basic android:layout_gravity="center", try add space element,using weights , .... in item_row.xml
didn't succeed :(
what have
what want
in activity
recyclerview.setlayoutmanager(new gridlayoutmanager(this, 3));
item_row.xml
<android.support.v7.widget.cardview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/cardview" android:layout_width="wrap_content" android:layout_height="wrap_content" card_view:cardcornerradius="2dp" card_view:cardelevation="4dp" card_view:cardusecompatpadding="true"> <!-- recycler view item row --> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <imageview android:id="@+id/imgthumbnail" android:layout_width="match_parent" android:layout_height="match_parent" android:adjustviewbounds="true" android:background="@android:color/black" /> <linearlayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="item name" android:id="@+id/txtname" android:layout_margin="5dp" android:singleline="false" android:lines="2" android:gravity="center" /> <linearlayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:layout_width="0dp" android:layout_height="match_parent" android:textappearance="?android:attr/textappearancesmall" android:id="@+id/txtcategory" android:textstyle="italic" android:textcolor="#496fa9" android:text="subtitle" android:layout_weight="1" android:gravity="center_vertical" android:paddingleft="10dp" android:layout_gravity="center" /> <imagebutton android:id="@+id/imagebutton" android:layout_width="48dp" android:layout_height="wrap_content" android:background="@android:color/black" android:adjustviewbounds="true" android:scaletype="fitcenter" android:padding="8dp"/> </linearlayout> </linearlayout> </linearlayout> </android.support.v7.widget.cardview>
i googled lot , found answer don't know how create linearlayout
each row of items!
is best solution?
any other idea?
you can use spansizelookup make views/items different widths grid view. can cleverness empty views adjust accordingly. might more complicate or simpler depending on how variable layout is.
for specific case show on drawing like:
gridlayoutmanager lm = new gridlayoutmanager(context, 6); lm.setspansizelookup(new mysizelookup()); public static class mysizelookup extends spansizelookup { public int getspansize(int position) { if(position >= 0 && position <= 2) { return 2; } else if (position == 3 || position == 6) { return 1; } else { return 2; } } }
and you'll have make on adapter, position 3 , 6 empty/invisible element. new view(context);
occupy space
Comments
Post a Comment