Android: Dynamic column/grid for ListView
public class DynamicColumnGridManager extends GridLayoutManager {
private int minItemWidth;
public DynamicColumnGridManager(Context context, int minItemWidth) {
super(context, 1);
this.minItemWidth = minItemWidth;
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler,
RecyclerView.State state) {
updateSpanCount();
super.onLayoutChildren(recycler, state);
}
private void updateSpanCount() {
int spanCount = getWidth() / minItemWidth;
if (spanCount < 1) {
spanCount = 1;
}
this.setSpanCount(spanCount);
}
}
mLayoutManager = new DynamicColumnGridManager(getApplicationContext(), mItemWidth);
mRecyclerView.setLayoutManager(mLayoutManager);
private int minItemWidth;
public DynamicColumnGridManager(Context context, int minItemWidth) {
super(context, 1);
this.minItemWidth = minItemWidth;
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler,
RecyclerView.State state) {
updateSpanCount();
super.onLayoutChildren(recycler, state);
}
private void updateSpanCount() {
int spanCount = getWidth() / minItemWidth;
if (spanCount < 1) {
spanCount = 1;
}
this.setSpanCount(spanCount);
}
}
mLayoutManager = new DynamicColumnGridManager(getApplicationContext(), mItemWidth);
mRecyclerView.setLayoutManager(mLayoutManager);
Comments
Post a Comment