Permalink
Browse files

Add Grid auto-column example

  • Loading branch information...
Luceefer
Luceefer committed Nov 19, 2015
1 parent e1da326 commit 57eea45f0b39676ba40d1a06e1e5d8c15c372983
Showing with 57 additions and 3 deletions.
  1. +2 −2 app/build.gradle
  2. +55 −1 app/src/main/java/com/codentrick/recyclerviewcollection/ui/GridActivity.java
@@ -21,6 +21,6 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:design:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
}
@@ -1,8 +1,10 @@
package com.codentrick.recyclerviewcollection.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
@@ -21,8 +23,9 @@
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
GridAutofitLayoutManager layoutManager = new GridAutofitLayoutManager(this, 160);
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(new SimpleAdapter());
}

@@ -54,4 +57,55 @@ public void onBindViewHolder(SimpleHolder simpleHolder, int i) {
simpleHolder.mTextView.setText(DataProvider.JAVA_BOOKS[i]);
}
}

public static class GridAutofitLayoutManager extends GridLayoutManager {

private int mColumnWidth;
private boolean mColumnWidthChanged = true;

public GridAutofitLayoutManager(Context context, int columnWidth) {
super(context, 1);
setColumnWidth(checkedColumnWidth(context, columnWidth));
}

public GridAutofitLayoutManager(Context context, int columnWidth, int orientation, boolean reverseLayout) {
/* Initially set spanCount to 1, will be changed automatically later. */
super(context, 1, orientation, reverseLayout);
setColumnWidth(checkedColumnWidth(context, columnWidth));
}

private int checkedColumnWidth(Context context, int columnWidth) {
if (columnWidth <= 0) {
/* Set default columnWidth value (48dp here). It is better to move this constant
to static constant on top, but we need context to convert it to dp, so can't really
do so. */
columnWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48,
context.getResources().getDisplayMetrics());
}
return columnWidth;
}

public void setColumnWidth(int newColumnWidth) {
if (newColumnWidth > 0 && newColumnWidth != mColumnWidth) {
mColumnWidth = newColumnWidth;
mColumnWidthChanged = true;
}
}

@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
if (mColumnWidthChanged && mColumnWidth > 0) {
int totalSpace;
if (getOrientation() == VERTICAL) {
totalSpace = getWidth() - getPaddingRight() - getPaddingLeft();
} else {
totalSpace = getHeight() - getPaddingTop() - getPaddingBottom();
}
int spanCount = Math.max(1, totalSpace / mColumnWidth);
setSpanCount(spanCount);
mColumnWidthChanged = false;
}
super.onLayoutChildren(recycler, state);
}
}
}

1 comment on commit 57eea45

@engr-erum

This comment has been minimized.

Copy link

engr-erum commented on 57eea45 Sep 28, 2017

i dnt want multi line text for each column how can i achieve this output https://imgur.com/a/qDmBv?

Please sign in to comment.