Wednesday, 16 November 2016

Dynamically change ListView to GridView and Vice Versa using RecyclerView

We can change the RecyclerView from type of ListView to GridView in single line of code.

Video output of sample project

code is:

private void setLayoutManager() {
    if (mColumnCount <= 1) {
        mColumnCount = 1;
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
    } else {
        recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));

Video output of sample project:

I hope this post is useful to you. kindly share your feedback as comment here.

Simple RecyclerView example in Android

Simple RecyclerView example with filter option in Android

Simple recyclerview example with checkbox in Android

Source code on GitHub

Thank You