android – 如何设置交错网格跨度计数以使用可用的屏幕宽度?

前端之家收集整理的这篇文章主要介绍了android – 如何设置交错网格跨度计数以使用可用的屏幕宽度?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用垂直StaggeredGridLayoutManager来显示一些缩略图.每行包含1英寸宽和150度高的卡片视图.我的问题是如何设置交错网格跨度计数以使用屏幕的可用物理宽度?

CardViewRow.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:card_view="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:layout_margin="5dp"
  7. android:orientation="vertical"
  8. card_view:cardCornerRadius="0dp"
  9. card_view:cardUseCompatPadding="true" >
  10.  
  11.  
  12. <LinearLayout
  13. android:gravity="center"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:background="@drawable/card_selector"
  17. android:orientation="vertical">
  18. <FrameLayout
  19. android:layout_width="match_parent"
  20. android:layout_height="match_parent">
  21.  
  22.  
  23. <ImageView
  24. android:id="@+id/thumbnail"
  25. android:layout_width="300dp"
  26. android:layout_height="150dp"
  27. android:scaleType="centerCrop"/>
  28.  
  29. <LinearLayout
  30. android:weightSum="2"
  31. android:padding="5dp"
  32. android:layout_gravity="bottom"
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:orientation="horizontal"
  36. android:background="#80000000">
  37.  
  38. <TextView
  39. android:id="@+id/size_txt"
  40. android:layout_weight="1"
  41. android:textSize="12sp"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:textColor="@color/silver"
  45. android:gravity="left"/>
  46. <TextView
  47. android:layout_weight="1"
  48. android:gravity="right"
  49. android:textColor="@color/silver"
  50. android:id="@+id/time_txt"
  51. android:textSize="12sp"
  52. android:layout_width="wrap_content"
  53. android:layout_height="wrap_content"/>
  54.  
  55. </LinearLayout>
  56. <View
  57. android:id="@+id/selector"
  58. android:visibility="invisible"
  59. android:background="#950096ff"
  60. android:layout_width="match_parent"
  61. android:layout_height="match_parent">
  62.  
  63. </View>
  64. </FrameLayout>
  65. <TextView
  66. android:id="@+id/title_txt"
  67. android:padding="8dp"
  68. android:layout_width="match_parent"
  69. android:layout_height="wrap_content"
  70. android:maxLines="5" />
  71. </LinearLayout>
  72.  
  73.  
  74. </android.support.v7.widget.CardView>

目前我正在使用此代码. (致谢:mstrengis)

  1. DisplayMetrics metrics = new DisplayMetrics();
  2. getWindowManager().getDefaultDisplay().getMetrics(metrics);
  3. int cardWidth =(int) metrics.xdpi; //CardWidth==1Inch.
  4. int spans = (int) Math.floor(mRecyclerView.getContext().getResources().getDisplayMetrics().widthPixels / (float) cardWidth);
  5. sglm.setSpanCount(spans);

/ *这完全适用于我的nexus 5和三星标签,但在我的lenovo手机上,卡宽度小于1.5CM,跨度计数超出我的预期.* /

解决方法

  1. int spans = (int) Math.floor(recyclerView.getContext().getResources().getDisplayMetrics().widthPixels / (float) cardWidth);
  2. RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
  3. if(layoutManager instanceof StaggeredGridLayoutManager) {
  4. ((StaggeredGridLayoutManager) layoutManager).setSpanCount(spans);
  5. }

然后使用RecyclerView.ItemDecoration在跨距之间添加间距

猜你在找的Android相关文章