RecyclerView: GridView and GridLayoutManager

Overview

RecyclerView is a powerful view of Android library. Developers can use it to replace ListView, GridView, and StaggeredGridView. Moreover we can add header, footer, special row, … by changing span size.

How to implement

grid-header                    Screenshot_2016-02-18-18-30-50

This grid layout has 2 column with special header in the first row. We can implement this grid layout by GridLayoutManager:

        GridLayoutManager manager = new GridLayoutManager(this, 2);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 0) {
                    return 2;
                } else {
                    return 1;
                }
            }
        });
        this.recyclerView.setLayoutManager(manager);

With above code, the first row (position = 0) will be header that spans 2 columns. Another rows has 2 columns.

Conclusion

With  RecyclerView, we can edit size of each row by GridLayoutManager. In conclusion, RecyclerView is a powerful and flexible library.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s