How to set staggered grid span count to use available screen width?
Asked Answered
M

1

8

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.*/

Monocyclic answered 16/8, 2015 at 7:59 Comment(1)
have you checked android developer doc for this layout ? StaggeredGridLayoutManager take look at this function setGapStrategy(int)Algiers
M
5
    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

Martyr answered 25/8, 2015 at 19:48 Comment(1)
try this method for calculating density pixels pastie.org/10378788 cardWidth = dp(75); //Aproximetly 1inch cardHeight = dp(150);Martyr

© 2022 - 2024 — McMap. All rights reserved.