How to make columns responsive to GridLayoutManager?

0

In case it is necessary that, when in portrait, it presents a number of columns that complete the width of the screen and when in landscape the items in the recyclerview are presented in a number of columns proportional to the new width, avoiding that the proportions of the cardviews.

    
asked by anonymous 28.11.2017 / 17:52

1 answer

0

Use getRotation() :

WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

if (manager != null) {
    Display display = manager.getDefaultDisplay();
    int rotation = display.getRotation();

    if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) {
        //em LADNSCAPE atribuimos uma quantidade de colunas maior a sua recycler view
        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
    } else {
        //em PORTRAIT uma quantidade menor
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
    }
}

You can run this code on your onCreate , when setting up your UI.

Link to class documentation: Display

    
29.11.2017 / 14:33