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);

Comments

Popular posts from this blog

Working with Android Hierarchy Viewer

Android: Standalone login with SQLiteOpenHelper