Skip to content

Commit 57eea45

Browse files
author
Luceefer
committed
Add Grid auto-column example
1 parent e1da326 commit 57eea45

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ android {
2121

2222
dependencies {
2323
compile fileTree(dir: 'libs', include: ['*.jar'])
24-
compile 'com.android.support:design:23.0.1'
25-
compile 'com.android.support:recyclerview-v7:23.0.1'
24+
compile 'com.android.support:design:23.1.1'
25+
compile 'com.android.support:recyclerview-v7:23.1.1'
2626
}

app/src/main/java/com/codentrick/recyclerviewcollection/ui/GridActivity.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.codentrick.recyclerviewcollection.ui;
22

3+
import android.content.Context;
34
import android.os.Bundle;
45
import android.support.v7.widget.GridLayoutManager;
56
import android.support.v7.widget.RecyclerView;
7+
import android.util.TypedValue;
68
import android.view.View;
79
import android.view.ViewGroup;
810
import 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

Comments
 (0)