Java Code Examples for androidx.recyclerview.widget.GridLayoutManager#SpanSizeLookup

The following examples show how to use androidx.recyclerview.widget.GridLayoutManager#SpanSizeLookup . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FlexibleDividerDecoration.java    From DoraemonKit with Apache License 2.0 6 votes vote down vote up
/**
 * @param manager
 * @param position
 * @return
 */
protected int positionTotalSpanSize(GridLayoutManager manager, int position) {
    int totalSpanSize = 0;
    GridLayoutManager.SpanSizeLookup spanSizeLookup = manager.getSpanSizeLookup();
    int spanCount = manager.getSpanCount();
    int groupIndex = spanSizeLookup.getSpanGroupIndex(position, spanCount);
    for (int i = position; i >= 0; i--) {
        int thisGroupIndex = spanSizeLookup.getSpanGroupIndex(i, spanCount);
        if (thisGroupIndex == groupIndex) {
            totalSpanSize += spanSizeLookup.getSpanSize(i);
        } else {
            break;
        }
    }
    return totalSpanSize;
}
 
Example 2
Source File: ExpandableAdapter.java    From SwipeRecyclerView with Apache License 2.0 6 votes vote down vote up
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
    RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();
    if (lm instanceof GridLayoutManager) {
        final GridLayoutManager glm = (GridLayoutManager)lm;
        final GridLayoutManager.SpanSizeLookup originLookup = glm.getSpanSizeLookup();

        glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (isParentItem(position)) return glm.getSpanCount();
                if (originLookup != null) return originLookup.getSpanSize(position);
                return 1;
            }
        });
    }
}
 
Example 3
Source File: SwipeRecyclerView.java    From SwipeRecyclerView with Apache License 2.0 6 votes vote down vote up
@Override
public void setLayoutManager(LayoutManager layoutManager) {
    if (layoutManager instanceof GridLayoutManager) {
        final GridLayoutManager gridLayoutManager = (GridLayoutManager)layoutManager;
        final GridLayoutManager.SpanSizeLookup spanSizeLookupHolder = gridLayoutManager.getSpanSizeLookup();

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (mAdapterWrapper.isHeader(position) || mAdapterWrapper.isFooter(position)) {
                    return gridLayoutManager.getSpanCount();
                }
                if (spanSizeLookupHolder != null) {
                    return spanSizeLookupHolder.getSpanSize(position - getHeaderCount());
                }
                return 1;
            }
        });
    }
    super.setLayoutManager(layoutManager);
}
 
Example 4
Source File: AdapterWrapper.java    From SwipeRecyclerView with Apache License 2.0 6 votes vote down vote up
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
    mAdapter.onAttachedToRecyclerView(recyclerView);

    RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();
    if (lm instanceof GridLayoutManager) {
        final GridLayoutManager glm = (GridLayoutManager)lm;
        final GridLayoutManager.SpanSizeLookup originLookup = glm.getSpanSizeLookup();

        glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (isHeaderOrFooter(position)) return glm.getSpanCount();
                if (originLookup != null) return originLookup.getSpanSize(position);
                return 1;
            }
        });
    }
}
 
Example 5
Source File: SubsonicFragment.java    From Audinaut with GNU General Public License v3.0 6 votes vote down vote up
GridLayoutManager.SpanSizeLookup getSpanSizeLookup(final GridLayoutManager gridLayoutManager) {
    return new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            SectionAdapter adapter = getCurrentAdapter();
            if (adapter != null) {
                int viewType = adapter.getItemViewType(position);
                if (viewType == SectionAdapter.VIEW_TYPE_HEADER) {
                    return gridLayoutManager.getSpanCount();
                } else {
                    return 1;
                }
            } else {
                return 1;
            }
        }
    };
}
 
Example 6
Source File: FlexibleDividerDecoration.java    From DoraemonKit with Apache License 2.0 6 votes vote down vote up
/**
 * In the case mShowLastDivider = false,
 * Returns offset for how many views we don't have to draw a divider for,
 * for LinearLayoutManager it is as simple as not drawing the last child divider,
 * but for a GridLayoutManager it needs to take the span count for the last items into account
 * until we use the span count configured for the grid.
 *
 * @param parent RecyclerView
 * @return offset for how many views we don't have to draw a divider or 1 if its a
 * LinearLayoutManager
 */
private int getLastDividerOffset(RecyclerView parent) {
    if (parent.getLayoutManager() instanceof GridLayoutManager) {
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
        GridLayoutManager.SpanSizeLookup spanSizeLookup = layoutManager.getSpanSizeLookup();
        int spanCount = layoutManager.getSpanCount();
        int itemCount = parent.getAdapter().getItemCount();
        for (int i = itemCount - 1; i >= 0; i--) {
            if (spanSizeLookup.getSpanIndex(i, spanCount) == 0) {
                return itemCount - i;
            }
        }
    }

    return 1;
}
 
Example 7
Source File: FlexibleDividerDecoration.java    From DoraemonKit with Apache License 2.0 6 votes vote down vote up
/**
 * @param manager
 * @param position
 * @return
 */
protected int positionTotalSpanSize(GridLayoutManager manager, int position) {
    int totalSpanSize = 0;
    GridLayoutManager.SpanSizeLookup spanSizeLookup = manager.getSpanSizeLookup();
    int spanCount = manager.getSpanCount();
    int groupIndex = spanSizeLookup.getSpanGroupIndex(position, spanCount);
    for (int i = position; i >= 0; i--) {
        int thisGroupIndex = spanSizeLookup.getSpanGroupIndex(i, spanCount);
        if (thisGroupIndex == groupIndex) {
            totalSpanSize += spanSizeLookup.getSpanSize(i);
        } else {
            break;
        }
    }
    return totalSpanSize;
}
 
Example 8
Source File: FlexibleDividerDecoration.java    From DoraemonKit with Apache License 2.0 6 votes vote down vote up
/**
 * In the case mShowLastDivider = false,
 * Returns offset for how many views we don't have to draw a divider for,
 * for LinearLayoutManager it is as simple as not drawing the last child divider,
 * but for a GridLayoutManager it needs to take the span count for the last items into account
 * until we use the span count configured for the grid.
 *
 * @param parent RecyclerView
 * @return offset for how many views we don't have to draw a divider or 1 if its a
 * LinearLayoutManager
 */
private int getLastDividerOffset(RecyclerView parent) {
    if (parent.getLayoutManager() instanceof GridLayoutManager) {
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
        GridLayoutManager.SpanSizeLookup spanSizeLookup = layoutManager.getSpanSizeLookup();
        int spanCount = layoutManager.getSpanCount();
        int itemCount = parent.getAdapter().getItemCount();
        for (int i = itemCount - 1; i >= 0; i--) {
            if (spanSizeLookup.getSpanIndex(i, spanCount) == 0) {
                return itemCount - i;
            }
        }
    }

    return 1;
}
 
Example 9
Source File: GridLayoutInfoTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testGridSpanSizeLookup() {
  final GridLayoutInfo gridLayoutInfo = createGridLayoutInfo(HORIZONTAL, 3);

  final RenderInfo renderInfo1 = mock(RenderInfo.class);
  when(renderInfo1.isFullSpan()).thenReturn(true);
  when(renderInfo1.getSpanSize()).thenReturn(1);

  final RenderInfo renderInfo2 = mock(RenderInfo.class);
  when(renderInfo2.isFullSpan()).thenReturn(false);
  when(renderInfo2.getSpanSize()).thenReturn(2);

  final RenderInfo renderInfo3 = mock(RenderInfo.class);
  when(renderInfo3.isFullSpan()).thenReturn(false);
  when(renderInfo3.getSpanSize()).thenReturn(1);

  final LayoutInfo.RenderInfoCollection renderInfoCollection =
      mock(LayoutInfo.RenderInfoCollection.class);
  when(renderInfoCollection.getRenderInfoAt(0)).thenReturn(renderInfo1);
  when(renderInfoCollection.getRenderInfoAt(1)).thenReturn(renderInfo2);
  when(renderInfoCollection.getRenderInfoAt(2)).thenReturn(renderInfo3);
  gridLayoutInfo.setRenderInfoCollection(renderInfoCollection);

  final GridLayoutManager.SpanSizeLookup spanSizeLookup =
      ((GridLayoutManager) gridLayoutInfo.getLayoutManager()).getSpanSizeLookup();
  assertThat(spanSizeLookup.getSpanSize(0)).isEqualTo(3);
  assertThat(spanSizeLookup.getSpanSize(1)).isEqualTo(2);
  assertThat(spanSizeLookup.getSpanSize(2)).isEqualTo(1);
}
 
Example 10
Source File: SelectDirectoryFragment.java    From Audinaut with GNU General Public License v3.0 5 votes vote down vote up
@Override
public GridLayoutManager.SpanSizeLookup getSpanSizeLookup(final GridLayoutManager gridLayoutManager) {
    return new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            int viewType = entryGridAdapter.getItemViewType(position);
            if (viewType == EntryGridAdapter.VIEW_TYPE_SONG || viewType == EntryGridAdapter.VIEW_TYPE_HEADER || viewType == EntryInfiniteGridAdapter.VIEW_TYPE_LOADING) {
                return gridLayoutManager.getSpanCount();
            } else {
                return 1;
            }
        }
    };
}
 
Example 11
Source File: SubsonicFragment.java    From Audinaut with GNU General Public License v3.0 5 votes vote down vote up
private GridLayoutManager getGridLayoutManager(RecyclerView recyclerView) {
    final int columns = getRecyclerColumnCount();
    GridLayoutManager gridLayoutManager = new GridLayoutManager(context, columns);

    GridLayoutManager.SpanSizeLookup spanSizeLookup = getSpanSizeLookup(gridLayoutManager);
    if (spanSizeLookup != null) {
        gridLayoutManager.setSpanSizeLookup(spanSizeLookup);
    }
    RecyclerView.ItemDecoration itemDecoration = getItemDecoration();
    recyclerView.addItemDecoration(itemDecoration);
    return gridLayoutManager;
}
 
Example 12
Source File: SearchFragment.java    From Audinaut with GNU General Public License v3.0 5 votes vote down vote up
@Override
public GridLayoutManager.SpanSizeLookup getSpanSizeLookup(final GridLayoutManager gridLayoutManager) {
    return new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            int viewType = adapter.getItemViewType(position);
            if (viewType == EntryGridAdapter.VIEW_TYPE_SONG || viewType == EntryGridAdapter.VIEW_TYPE_HEADER || viewType == ArtistAdapter.VIEW_TYPE_ARTIST) {
                return gridLayoutManager.getSpanCount();
            } else {
                return 1;
            }
        }
    };
}
 
Example 13
Source File: GridSpacingDecoration.java    From Audinaut with GNU General Public License v3.0 5 votes vote down vote up
private int getSpanSize(RecyclerView parent, int childIndex) {
    RecyclerView.LayoutManager mgr = parent.getLayoutManager();
    if (mgr instanceof GridLayoutManager) {
        GridLayoutManager.SpanSizeLookup lookup = ((GridLayoutManager) mgr).getSpanSizeLookup();
        if (lookup != null) {
            return lookup.getSpanSize(childIndex);
        }
    }

    return 1;
}
 
Example 14
Source File: PhotoInfoAdapter3.java    From Mysplash with GNU Lesser General Public License v3.0 5 votes vote down vote up
public GridLayoutManager.SpanSizeLookup getSpanSizeLookup(boolean landscape) {
    return new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            if (viewModelList.get(position) instanceof ExifModel) {
                return landscape ? columnCount / 2 : 1;
            } else {
                return columnCount;
            }
        }
    };
}
 
Example 15
Source File: GridDividerDecoration.java    From pandora with Apache License 2.0 5 votes vote down vote up
private int getGroupIndex(int position, RecyclerView parent) {
    if (parent.getLayoutManager() instanceof GridLayoutManager) {
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
        GridLayoutManager.SpanSizeLookup spanSizeLookup = layoutManager.getSpanSizeLookup();
        int spanCount = layoutManager.getSpanCount();
        return spanSizeLookup.getSpanGroupIndex(position, spanCount);
    }

    return position;
}
 
Example 16
Source File: ArtistViewModel.java    From Jockey with Apache License 2.0 5 votes vote down vote up
@Bindable
public LayoutManager getLayoutManager() {
    mAlbumColumnCount = ViewUtils.getNumberOfGridColumns(getContext(), R.dimen.grid_width);
    mRelatedColumnCount = ViewUtils.getNumberOfGridColumns(getContext(), R.dimen.large_grid_width);
    mColumnCount = mAlbumColumnCount * mRelatedColumnCount;

    // Setup the GridLayoutManager
    GridLayoutManager layoutManager = new GridLayoutManager(getContext(), mColumnCount);
    GridLayoutManager.SpanSizeLookup spanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            // Albums & related artists fill one column,
            // all other view types fill the available width
            boolean isArtist = mAlbumSection != null
                    && mAdapter.getItemViewType(position) == mAlbumSection.getTypeId();
            boolean isRelatedArtist = mRelatedArtistSection != null
                    && mAdapter.getItemViewType(position) == mRelatedArtistSection.getTypeId();

            if (isArtist) {
                return mRelatedColumnCount;
            } else if (isRelatedArtist) {
                return mAlbumColumnCount;
            } else {
                return mColumnCount;
            }
        }
    };

    spanSizeLookup.setSpanIndexCacheEnabled(true);
    layoutManager.setSpanSizeLookup(spanSizeLookup);

    return layoutManager;
}
 
Example 17
Source File: BasicGridLayoutManager.java    From UltimateRecyclerView with Apache License 2.0 4 votes vote down vote up
protected GridLayoutManager.SpanSizeLookup decideSpanSizeCal() {
    return mSpanSizeLookUp;
}
 
Example 18
Source File: WeatherIconAdapter.java    From GeometricWeather with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static GridLayoutManager.SpanSizeLookup getSpanSizeLookup(int columnCount,
                                                                 List<WeatherIconAdapter.Item> itemList) {
    return new SpanSizeLookup(columnCount, itemList);
}
 
Example 19
Source File: GridSpacingDecoration.java    From Audinaut with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);

    int spacing = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, SPACING, view.getResources().getDisplayMetrics());
    int halfSpacing = spacing / 2;

    int childCount = parent.getChildCount();
    int childIndex = parent.getChildAdapterPosition(view);
    // Not an actual child (ie: during delete event)
    if (childIndex == -1) {
        return;
    }
    int spanCount = getTotalSpan(parent);
    int spanIndex = childIndex % spanCount;

    // If we can, use the SpanSizeLookup since headers screw up the index calculation
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();
        if (spanSizeLookup != null) {
            spanIndex = spanSizeLookup.getSpanIndex(childIndex, spanCount);
        }
    }
    int spanSize = getSpanSize(parent, childIndex);

    /* INVALID SPAN */
    if (spanCount < 1 || spanSize > 1) return;

    int margins = 0;
    if (view instanceof UpdateView) {
        View firstChild = ((ViewGroup) view).getChildAt(0);
        ViewGroup.LayoutParams layoutParams = firstChild.getLayoutParams();
        if (layoutParams instanceof LinearLayout.LayoutParams) {
            margins = ((LinearLayout.LayoutParams) layoutParams).bottomMargin;
        } else if (layoutParams instanceof FrameLayout.LayoutParams) {
            margins = ((FrameLayout.LayoutParams) layoutParams).bottomMargin;
        }
    }
    int doubleMargins = margins * 2;

    outRect.top = halfSpacing - margins;
    outRect.bottom = halfSpacing - margins;
    outRect.left = halfSpacing - margins;
    outRect.right = halfSpacing - margins;

    if (isTopEdge(childIndex, spanIndex, spanCount)) {
        outRect.top = spacing - doubleMargins;
    }

    if (isLeftEdge(spanIndex)) {
        outRect.left = spacing - doubleMargins;
    }

    if (isRightEdge(spanIndex, spanCount)) {
        outRect.right = spacing - doubleMargins;
    }

    if (isBottomEdge(childIndex, childCount, spanCount)) {
        outRect.bottom = spacing - doubleMargins;
    }
}
 
Example 20
Source File: GridRecyclerViewFragment.java    From cathode with Apache License 2.0 4 votes vote down vote up
protected GridLayoutManager.SpanSizeLookup getSpanSizeLookup() {
  return null;
}