Search code examples
android-recyclerviewgridlayoutmanager

How to determine span index of grid layout manager?


I'am using RecyclerView and GridLayoutManager with 5 span count in my layout.

How to determine span index of item in GridLayoutManager by the item position?


Solution

  • Finally i found it! The layout param of root view holder item is GridLayoutManager.LayoutParams and has getSpanIndex() method.

    RecyclerView.ViewHolder viewHolder = m_RecyclerView.findViewHolderForAdapterPosition(position);
    if (viewHolder != null) {
        GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) viewHolder.itemView.getLayoutParams();
        int spanIndex = lp.getSpanIndex();
    }