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

The following examples show how to use android.support.v7.widget.GridLayoutManager#getSpanCount() . 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: MaterialViewPagerHeaderDecorator.java    From MaterialViewPager with Apache License 2.0 6 votes vote down vote up
@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));
        }
    }
}
 
Example 2
Source File: BasePageIndicator.java    From GridPagerSnapHelper with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: BasePageIndicator.java    From GridPagerSnapHelper with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 4
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 5
Source File: GridLayoutManagerColumnCountAssertion.java    From espresso-macchiato with MIT License 6 votes vote down vote up
@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");
    }
}
 
Example 6
Source File: SelectPodcastsFragment.java    From Popeens-DSub with GNU General Public License v3.0 6 votes vote down vote up
@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;
			}
		}
	};
}
 
Example 7
Source File: RecyclerViewColumnMatcher.java    From material-activity-chooser with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 8
Source File: PowerAdapter.java    From PowerRecyclerView with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 9
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 10
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 11
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 12
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 13
Source File: BaseDecoration.java    From StickyDecoration with Apache License 2.0 5 votes vote down vote up
/**
 * 网格布局需要调用
 *
 * @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);
}
 
Example 14
Source File: FamiliarDefaultItemDecoration.java    From AndroidBase with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 15
Source File: RecyclerViewTouchHelper.java    From YImagePicker with Apache License 2.0 5 votes vote down vote up
private int getSpanCount() {
    if (spanCount != 0) {
        return spanCount;
    }
    GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
    if (gridLayoutManager != null) {
        spanCount = gridLayoutManager.getSpanCount();
        return spanCount;
    }
    return 0;
}
 
Example 16
Source File: SharpGridScaler.java    From MultiView with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 17
Source File: IndexableLayout.java    From IndexableRecyclerView with Apache License 2.0 4 votes vote down vote up
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);
            }
        }
    }
}
 
Example 18
Source File: SpanSizeLookupWrapper.java    From SupportSwipeLoadmore with GNU General Public License v3.0 4 votes vote down vote up
public SpanSizeLookupWrapper(GridLayoutManager layoutManager,
                             RecyclerFooterAdapterWrapper adapter) {
    this.wrapper = layoutManager.getSpanSizeLookup();
    this.spanSize = layoutManager.getSpanCount();
    this.adapter = adapter;
}
 
Example 19
Source File: EndlessRecyclerScrollListener.java    From redux-android-sample with Apache License 2.0 4 votes vote down vote up
public EndlessRecyclerScrollListener(GridLayoutManager layoutManager) {
    mLayoutManager = layoutManager;
    mVisibleThreshold = mVisibleThreshold * layoutManager.getSpanCount();
}
 
Example 20
Source File: EndlessRecyclerViewScrollListener.java    From playa with MIT License 4 votes vote down vote up
public EndlessRecyclerViewScrollListener(GridLayoutManager layoutManager) {
    this.mLayoutManager = layoutManager;
    visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}