下面列出了android.support.v7.widget.GridLayoutManager#getSpanCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public GridLayoutManager.SpanSizeLookup getSpanSizeLookup(final GridLayoutManager gridLayoutManager) {
return new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
SectionAdapter adapter = getCurrentAdapter();
if(adapter != null) {
int viewType = getCurrentAdapter().getItemViewType(position);
if (viewType == SectionAdapter.VIEW_TYPE_HEADER || viewType == PodcastChannelAdapter.VIEW_TYPE_PODCAST_EPISODE || viewType == PodcastChannelAdapter.VIEW_TYPE_PODCAST_LEGACY) {
return gridLayoutManager.getSpanCount();
} else {
return 1;
}
} else {
return 1;
}
}
};
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView recyclerView, RecyclerView.State state) {
final RecyclerView.ViewHolder holder = recyclerView.getChildViewHolder(view);
final Context context = recyclerView.getContext();
if(!registered) {
MaterialViewPagerHelper.registerRecyclerView(context, recyclerView);
registered = true;
}
int headerCells = 1;
//don't work with stagged
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if(layoutManager instanceof GridLayoutManager){
GridLayoutManager gridLayoutManager = (GridLayoutManager)layoutManager;
headerCells = gridLayoutManager.getSpanCount();
}
MaterialViewPagerAnimator animator = MaterialViewPagerHelper.getAnimator(context);
if (animator != null) {
if (holder.getAdapterPosition() < headerCells) {
outRect.top = Math.round(Utils.dpToPx(animator.getHeaderHeight() + 10, context));
}
}
}
/**
* 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;
}
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
if (noViewFoundException != null) {
throw noViewFoundException;
}
RecyclerView recyclerView = (RecyclerView) view;
if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
int spanCount = gridLayoutManager.getSpanCount();
if (spanCount != expectedColumnCount) {
String errorMessage = "expected column count " + expectedColumnCount
+ " but was " + spanCount;
throw new AssertionError(errorMessage);
}
} else {
throw new IllegalStateException("no grid layout manager");
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
onPageScrollStateChanged(newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
int position = 0;
if (layoutManager instanceof GridLayoutManager) {
GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
position = gridLayoutManager.findFirstVisibleItemPosition();
int row = gridLayoutManager.getSpanCount();
position = position / (row * mPageColumn);
} else if (layoutManager instanceof LinearLayoutManager) {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
position = linearLayoutManager.findFirstVisibleItemPosition();
}
onPageSelected(position);
}
}
/**
* The number of each page
*
* @return
*/
protected int eachPageItemCount() {
if (mRecyclerView == null) {
return 0;
}
int row = 1;
RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
if (layoutManager != null) {
if (layoutManager instanceof GridLayoutManager) {
GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
row = gridLayoutManager.getSpanCount();
}
}
return row * mPageColumn;
}
@Override
public void scale(Float currScale, Integer currSpan, MotionEvent ev) {
Log.d("scale: " + currScale + "/" + currSpan + " :: " + ev);
GridLayoutManager lm = (GridLayoutManager)getRecyclerView().getLayoutManager();
if(lm.getSpanCount() !=currSpan) {
lm.setSpanCount(currSpan);
getRecyclerView().getLayoutManager().requestLayout();
}
ViewCompat.setScaleX(getRecyclerView(),currScale);
ViewCompat.setScaleY(getRecyclerView(),currScale);
getRecyclerView().invalidate();
}
private int getSpanCount() {
if (spanCount != 0) {
return spanCount;
}
GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
if (gridLayoutManager != null) {
spanCount = gridLayoutManager.getSpanCount();
return spanCount;
}
return 0;
}
private void initLayoutManagerType() {
this.mLayoutManagerType = mRecyclerViewListener.getCurLayoutManagerType();
RecyclerView.LayoutManager layoutManager = mRecyclerViewListener.getCurLayoutManager();
switch (mLayoutManagerType) {
case FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_LINEAR:
LinearLayoutManager curLinearLayoutManager = (LinearLayoutManager)layoutManager;
if (curLinearLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
mOrientation = OrientationHelper.HORIZONTAL;
} else {
mOrientation = OrientationHelper.VERTICAL;
}
break;
case FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_GRID:
GridLayoutManager curGridLayoutManager = (GridLayoutManager)layoutManager;
mGridSpanCount = curGridLayoutManager.getSpanCount();
if (curGridLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
mOrientation = OrientationHelper.HORIZONTAL;
} else {
mOrientation = OrientationHelper.VERTICAL;
}
break;
case FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_STAGGERED_GRID:
StaggeredGridLayoutManager curStaggeredGridLayoutManager = (StaggeredGridLayoutManager)layoutManager;
mGridSpanCount = curStaggeredGridLayoutManager.getSpanCount();
if (curStaggeredGridLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
mOrientation = OrientationHelper.HORIZONTAL;
} else {
mOrientation = OrientationHelper.VERTICAL;
}
break;
default:
this.mLayoutManagerType = FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_LINEAR;
}
initDivisible();
}
/**
* 网格布局需要调用
*
* @param recyclerView recyclerView
* @param gridLayoutManager gridLayoutManager
*/
public void resetSpan(RecyclerView recyclerView, GridLayoutManager gridLayoutManager) {
if (recyclerView == null) {
throw new NullPointerException("recyclerView not allow null");
}
if (gridLayoutManager == null) {
throw new NullPointerException("gridLayoutManager not allow null");
}
final int spanCount = gridLayoutManager.getSpanCount();
//相当于weight
GridLayoutManager.SpanSizeLookup lookup = new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
int span;
int realPosition = getRealPosition(position);
if (realPosition < 0) {
//小于header数量
span = spanCount;
} else {
String curGroupId = getGroupName(realPosition);
String nextGroupId;
try {
//防止外面没判断,导致越界
nextGroupId = getGroupName(realPosition + 1);
} catch (Exception e) {
nextGroupId = curGroupId;
}
if (!TextUtils.equals(curGroupId, nextGroupId)) {
//为本行的最后一个
int posFirstInGroup = getFirstInGroupWithCash(realPosition);
span = spanCount - (realPosition - posFirstInGroup) % spanCount;
} else {
span = 1;
}
}
return span;
}
};
gridLayoutManager.setSpanSizeLookup(lookup);
}
/**
* 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;
}
@Override
protected boolean matchesSafely(View item) {
if (item instanceof RecyclerView) {
RecyclerView recyclerView = (RecyclerView) item;
if (recyclerView.getLayoutManager() != null && recyclerView.getLayoutManager() instanceof GridLayoutManager) {
GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
if (gridLayoutManager.getSpanCount() == columnCount) {
return true;
}
}
}
return false;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
private int initSpanSize(int position, GridLayoutManager manager) {
int itemViewType = getItemViewType(position);
switch (itemViewType) {
case AdapterLoader.TYPE_BOTTOM:
case AdapterLoader.TYPE_EMPTY:
case AdapterLoader.TYPE_ERROR:
return manager.getSpanCount();
default:
return getSpanSize(position);
}
}
public EndlessRecyclerViewScrollListener(GridLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}
private void processScrollListener() {
if (!(mLayoutManager instanceof LinearLayoutManager)) return;
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mLayoutManager;
int firstItemPosition;
firstItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
if (firstItemPosition == RecyclerView.NO_POSITION) return;
mIndexBar.setSelection(firstItemPosition);
if (!mSticyEnable) return;
ArrayList<EntityWrapper> list = mRealAdapter.getItems();
if (mStickyViewHolder != null && list.size() > firstItemPosition) {
EntityWrapper wrapper = list.get(firstItemPosition);
String wrapperTitle = wrapper.getIndexTitle();
if (EntityWrapper.TYPE_TITLE == wrapper.getItemType()) {
if (mLastInvisibleRecyclerViewItemView != null && mLastInvisibleRecyclerViewItemView.getVisibility() == INVISIBLE) {
mLastInvisibleRecyclerViewItemView.setVisibility(VISIBLE);
mLastInvisibleRecyclerViewItemView = null;
}
mLastInvisibleRecyclerViewItemView = linearLayoutManager.findViewByPosition(firstItemPosition);
if (mLastInvisibleRecyclerViewItemView != null) {
mLastInvisibleRecyclerViewItemView.setVisibility(INVISIBLE);
}
}
// hide -> show
if (wrapperTitle == null && mStickyViewHolder.itemView.getVisibility() == VISIBLE) {
mStickyTitle = null;
mStickyViewHolder.itemView.setVisibility(INVISIBLE);
} else {
stickyNewViewHolder(wrapperTitle);
}
// GirdLayoutManager
if (mLayoutManager instanceof GridLayoutManager) {
GridLayoutManager gridLayoutManager = (GridLayoutManager) mLayoutManager;
if (firstItemPosition + gridLayoutManager.getSpanCount() < list.size()) {
for (int i = firstItemPosition + 1; i <= firstItemPosition + gridLayoutManager.getSpanCount(); i++) {
processScroll(linearLayoutManager, list, i, wrapperTitle);
}
}
} else { // LinearLayoutManager
if (firstItemPosition + 1 < list.size()) {
processScroll(linearLayoutManager, list, firstItemPosition + 1, wrapperTitle);
}
}
}
}
public SpanSizeLookupWrapper(GridLayoutManager layoutManager,
RecyclerFooterAdapterWrapper adapter) {
this.wrapper = layoutManager.getSpanSizeLookup();
this.spanSize = layoutManager.getSpanCount();
this.adapter = adapter;
}
public EndlessRecyclerScrollListener(GridLayoutManager layoutManager) {
mLayoutManager = layoutManager;
mVisibleThreshold = mVisibleThreshold * layoutManager.getSpanCount();
}