11package com .codentrick .recyclerviewcollection .ui ;
22
3+ import android .content .Context ;
34import android .os .Bundle ;
45import android .support .v7 .widget .GridLayoutManager ;
56import android .support .v7 .widget .RecyclerView ;
7+ import android .util .TypedValue ;
68import android .view .View ;
79import android .view .ViewGroup ;
810import android .widget .TextView ;
@@ -21,8 +23,9 @@ public class GridActivity extends RecyclerViewActivity {
2123 protected void onCreate (Bundle savedInstanceState ) {
2224 super .onCreate (savedInstanceState );
2325 setContentView (R .layout .activity_simple );
26+ GridAutofitLayoutManager layoutManager = new GridAutofitLayoutManager (this , 160 );
2427 RecyclerView mRecyclerView = (RecyclerView ) findViewById (R .id .recycler_view );
25- mRecyclerView .setLayoutManager (new GridLayoutManager ( this , 3 ) );
28+ mRecyclerView .setLayoutManager (layoutManager );
2629 mRecyclerView .setAdapter (new SimpleAdapter ());
2730 }
2831
@@ -54,4 +57,55 @@ public void onBindViewHolder(SimpleHolder simpleHolder, int i) {
5457 simpleHolder .mTextView .setText (DataProvider .JAVA_BOOKS [i ]);
5558 }
5659 }
60+
61+ public static class GridAutofitLayoutManager extends GridLayoutManager {
62+
63+ private int mColumnWidth ;
64+ private boolean mColumnWidthChanged = true ;
65+
66+ public GridAutofitLayoutManager (Context context , int columnWidth ) {
67+ super (context , 1 );
68+ setColumnWidth (checkedColumnWidth (context , columnWidth ));
69+ }
70+
71+ public GridAutofitLayoutManager (Context context , int columnWidth , int orientation , boolean reverseLayout ) {
72+ /* Initially set spanCount to 1, will be changed automatically later. */
73+ super (context , 1 , orientation , reverseLayout );
74+ setColumnWidth (checkedColumnWidth (context , columnWidth ));
75+ }
76+
77+ private int checkedColumnWidth (Context context , int columnWidth ) {
78+ if (columnWidth <= 0 ) {
79+ /* Set default columnWidth value (48dp here). It is better to move this constant
80+ to static constant on top, but we need context to convert it to dp, so can't really
81+ do so. */
82+ columnWidth = (int ) TypedValue .applyDimension (TypedValue .COMPLEX_UNIT_DIP , 48 ,
83+ context .getResources ().getDisplayMetrics ());
84+ }
85+ return columnWidth ;
86+ }
87+
88+ public void setColumnWidth (int newColumnWidth ) {
89+ if (newColumnWidth > 0 && newColumnWidth != mColumnWidth ) {
90+ mColumnWidth = newColumnWidth ;
91+ mColumnWidthChanged = true ;
92+ }
93+ }
94+
95+ @ Override
96+ public void onLayoutChildren (RecyclerView .Recycler recycler , RecyclerView .State state ) {
97+ if (mColumnWidthChanged && mColumnWidth > 0 ) {
98+ int totalSpace ;
99+ if (getOrientation () == VERTICAL ) {
100+ totalSpace = getWidth () - getPaddingRight () - getPaddingLeft ();
101+ } else {
102+ totalSpace = getHeight () - getPaddingTop () - getPaddingBottom ();
103+ }
104+ int spanCount = Math .max (1 , totalSpace / mColumnWidth );
105+ setSpanCount (spanCount );
106+ mColumnWidthChanged = false ;
107+ }
108+ super .onLayoutChildren (recycler , state );
109+ }
110+ }
57111}
0 commit comments