Search code examples
androidandroid-recyclerviewgridlayoutmanager

How to get the number of spanCount(number of columns)


I am currently working on a refactoring task which requires replacing ListView(GridView) by RecyclerView.

In a part of the job, it requires to get the span count. As the former code has a method to get the number of columns of GridView like this:

public int getNumColumns()

Then I am wondering that if there is a same method in RecyclerView?

Your answers are highly appreciated.

Sincerely, Jack

I tried something like:

mRecyclerView.getLayoutManager().getColumnCountForAccessibility()

But apparently, this is only for the sake of horizontal scrolling.

public int getNumColumns()

Solution

  • Try using,

    GridLayoutManager layoutManager = (GridLayoutManager)mRecyclerView.getLayoutManager();
    int spanCount = layoutManager.getSpanCount()