I'm using vertical StaggeredGridLayoutManager to display some thumbnails. Each row contains a cardview with 1 inch width and 150dp height. My question is How can i set the staggered grid span count to use available physical width of the screen?
CardViewRow.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical"
card_view:cardCornerRadius="0dp"
card_view:cardUseCompatPadding="true" >
<LinearLayout
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/card_selector"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/thumbnail"
android:layout_width="300dp"
android:layout_height="150dp"
android:scaleType="centerCrop"/>
<LinearLayout
android:weightSum="2"
android:padding="5dp"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#80000000">
<TextView
android:id="@+id/size_txt"
android:layout_weight="1"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/silver"
android:gravity="left"/>
<TextView
android:layout_weight="1"
android:gravity="right"
android:textColor="@color/silver"
android:id="@+id/time_txt"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<View
android:id="@+id/selector"
android:visibility="invisible"
android:background="#950096ff"
android:layout_width="match_parent"
android:layout_height="match_parent">
</View>
</FrameLayout>
<TextView
android:id="@+id/title_txt"
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="5" />
</LinearLayout>
</android.support.v7.widget.CardView>
Currently I'm using this code. (Credits: mstrengis)
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int cardWidth =(int) metrics.xdpi; //CardWidth==1Inch.
int spans = (int) Math.floor(mRecyclerView.getContext().getResources().getDisplayMetrics().widthPixels / (float) cardWidth);
sglm.setSpanCount(spans);
/* This works perfectly on my nexus 5 and samsung tab, But on my lenovo phone the card width is less than 1.5CM and span count is more than what i expected.*/
int spans = (int) Math.floor(recyclerView.getContext().getResources().getDisplayMetrics().widthPixels / (float) cardWidth);
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if(layoutManager instanceof StaggeredGridLayoutManager) {
((StaggeredGridLayoutManager) layoutManager).setSpanCount(spans);
}
and then use RecyclerView.ItemDecoration
to add spacing between spans
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With