How to set the number of grid intervals in a checkerboard pattern to use the available screen width?

I use the vertical StaggeredGridLayoutManager to display some thumbnails. Each line contains a card with a width of 1 inch and a height of 150dp. My question is: how can I set the scale value in a checkerboard pattern to use the available physical screen width?

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> 

I am currently 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 fine on my fifth page 5 and samsung tab. But on my lenovo phone, the width of the card is less than 1.5CM, and the number of transitions is more than expected. * /

+7
android width
source share
1 answer
  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

+4
source share

All Articles