Java Code Examples for android.support.v7.widget.GridLayoutManager#getSpanSizeLookup()

The following examples show how to use android.support.v7.widget.GridLayoutManager#getSpanSizeLookup() . 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: SpaceItemDecoration.java    From JD-Test with Apache License 2.0 6 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    if(spanSizeLookup == null){
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
        spanSizeLookup = layoutManager.getSpanSizeLookup();
    }
    int spanSize = spanSizeLookup.getSpanSize(parent.getChildLayoutPosition(view));

    if( spanSize == 2){
        if(firstPosition == -1){
            firstPosition = parent.getChildLayoutPosition(view);
        }
        if((parent.getChildLayoutPosition(view) - firstPosition) % 2 == 0){
            outRect.right = space/2;
            outRect.top = space;
        }
        else{
            outRect.left = space/2;
            outRect.top = space;
        }

    }

}
 
Example 2
Source File: WrapperUtils.java    From likequanmintv with Apache License 2.0 6 votes vote down vote up
public static void onAttachedToRecyclerView(RecyclerView.Adapter innerAdapter, RecyclerView recyclerView, final SpanSizeCallback callback)
{
    innerAdapter.onAttachedToRecyclerView(recyclerView);

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager)
    {
        final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup()
        {
            @Override
            public int getSpanSize(int position)
            {
                return callback.getSpanSize(gridLayoutManager, spanSizeLookup, position);
            }
        });
        gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
    }
}
 
Example 3
Source File: EasyHeaderFooterAdapter.java    From easy-header-footer-adapter with MIT License 6 votes vote down vote up
private void initLayoutManager(final RecyclerView.LayoutManager layoutManager) {
    this.layoutManager = layoutManager;

    if (layoutManager instanceof GridLayoutManager) {
        final GridLayoutManager castedLayoutManager = (GridLayoutManager) layoutManager;
        final SpanSizeLookup existingLookup = castedLayoutManager.getSpanSizeLookup();

        castedLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (isHeader(position) || isFooter(position)) {
                    return castedLayoutManager.getSpanCount();
                }

                return existingLookup.getSpanSize(getRealPosition(position));
            }
        });
    }
}
 
Example 4
Source File: SwipeMenuRecyclerView.java    From SwipeRecyclerView-master 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.isHeaderView(position) || mAdapterWrapper.isFooterView(position)) {
                    return gridLayoutManager.getSpanCount();
                }
                if (spanSizeLookupHolder != null)
                    return spanSizeLookupHolder.getSpanSize(position - getHeaderItemCount());
                return 1;
            }
        });
    }
    super.setLayoutManager(layoutManager);
}
 
Example 5
Source File: WrapperUtils.java    From Bailan with Apache License 2.0 6 votes vote down vote up
public static void onAttachedToRecyclerView(RecyclerView.Adapter innerAdapter, RecyclerView recyclerView, final SpanSizeCallback callback) {
    innerAdapter.onAttachedToRecyclerView(recyclerView);

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return callback.getSpanSize(gridLayoutManager, spanSizeLookup, position);
            }
        });
        gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
    }
}
 
Example 6
Source File: FlexibleDividerDecoration.java    From AccountBook with GNU General Public License v3.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: WrapperUtils.java    From CoordinatorLayoutExample with Apache License 2.0 6 votes vote down vote up
public static void onAttachedToRecyclerView(RecyclerView.Adapter innerAdapter, RecyclerView
        recyclerView, final SpanSizeCallback callback) {
    innerAdapter.onAttachedToRecyclerView(recyclerView);

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager
                .getSpanSizeLookup();

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return callback.getSpanSize(gridLayoutManager, spanSizeLookup, position);
            }
        });
        gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
    }
}
 
Example 8
Source File: WrapperUtils.java    From Bailan with Apache License 2.0 6 votes vote down vote up
public static void onAttachedToRecyclerView(RecyclerView.Adapter innerAdapter, RecyclerView recyclerView, final SpanSizeCallback callback) {
    innerAdapter.onAttachedToRecyclerView(recyclerView);

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return callback.getSpanSize(gridLayoutManager, spanSizeLookup, position);
            }
        });
        gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
    }
}
 
Example 9
Source File: BaseDivider.java    From TitanRecyclerView with MIT License 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 10
Source File: FlexibleDividerDecoration.java    From AFBaseLibrary 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 11
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 12
Source File: BaseAdapter.java    From MvpRoute with Apache License 2.0 6 votes vote down vote up
/**
 * 解决GrildLayoutManager问题
 *
 * @param recyclerView
 */
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
	super.onAttachedToRecyclerView(recyclerView);
	RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
	if (layoutManager instanceof GridLayoutManager) {
		final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
		final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

		gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
			@Override
			public int getSpanSize(int position) {
				if (isHeaderPos(position)) {
					return gridLayoutManager.getSpanCount();
				} else if (isFooterPos(position)) {
					return gridLayoutManager.getSpanCount();
				}
				if (spanSizeLookup != null) {
					return spanSizeLookup.getSpanSize(position);
				}
				return 1;
			}
		});
		gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
	}
}
 
Example 13
Source File: BaseDivider.java    From TitanRecyclerView with MIT License 5 votes vote down vote up
/**
 * Determines whether divider was already drawn for the row the item is in,
 * effectively only makes sense for a grid
 *
 * @param position current view position to draw divider
 * @param parent   RecyclerView
 * @return true if the divider can be skipped as it is in the same row as the previous one.
 */
private boolean wasDividerAlreadyDrawn(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.getSpanIndex(position, spanCount) > 0;
    }

    return false;
}
 
Example 14
Source File: FlexibleDividerDecoration.java    From AFBaseLibrary with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a group index for GridLayoutManager.
 * for LinearLayoutManager, always returns position.
 *
 * @param position current view position to draw divider
 * @param parent   RecyclerView
 * @return group index of items
 */
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 15
Source File: FlexibleDividerDecoration.java    From RecyclerView-FlexibleDivider with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a group index for GridLayoutManager.
 * for LinearLayoutManager, always returns position.
 *
 * @param position current view position to draw divider
 * @param parent   RecyclerView
 * @return group index of items
 */
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: LoadMoreCommonAdapter.java    From likequanmintv with Apache License 2.0 5 votes vote down vote up
@Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView)
    {
//        Log.d("LoadMoreCommonAdapter","onAttachedToRecyclerView");
        super.onAttachedToRecyclerView(recyclerView);
        final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager)
        {
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup()
            {
                @Override
                public int getSpanSize(int position)
                {
                    if (isShowLoadMore(position))
                    {
                        return gridLayoutManager.getSpanCount();
                    }
                    if (spanSizeLookup != null)
                    {
                        return spanSizeLookup.getSpanSize(position);
                    }
                    return 1;
                }
            });
            gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
        }
    }
 
Example 17
Source File: FlexibleDividerDecoration.java    From AccountBook with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Determines whether divider was already drawn for the row the item is in,
 * effectively only makes sense for a grid
 *
 * @param position current view position to draw divider
 * @param parent   RecyclerView
 * @return true if the divider can be skipped as it is in the same row as the previous one.
 */
private boolean wasDividerAlreadyDrawn(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.getSpanIndex(position, spanCount) > 0;
    }

    return false;
}
 
Example 18
Source File: FlexibleDividerDecoration.java    From AccountBook with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a group index for GridLayoutManager.
 * for LinearLayoutManager, always returns position.
 *
 * @param position current view position to draw divider
 * @param parent   RecyclerView
 * @return group index of items
 */
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 19
Source File: FlexibleDividerDecoration.java    From RecyclerView-FlexibleDivider with Apache License 2.0 5 votes vote down vote up
/**
 * Determines whether divider was already drawn for the row the item is in,
 * effectively only makes sense for a grid
 *
 * @param position current view position to draw divider
 * @param parent   RecyclerView
 * @return true if the divider can be skipped as it is in the same row as the previous one.
 */
private boolean wasDividerAlreadyDrawn(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.getSpanIndex(position, spanCount) > 0;
    }

    return false;
}
 
Example 20
Source File: BaseDivider.java    From TitanRecyclerView with MIT License 5 votes vote down vote up
/**
 * Returns a group index for GridLayoutManager.
 * for LinearLayoutManager, always returns position.
 *
 * @param position current view position to draw divider
 * @param parent   RecyclerView
 * @return group index of items
 */
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;
}