Java Code Examples for androidx.recyclerview.widget.StaggeredGridLayoutManager#getSpanCount()

The following examples show how to use androidx.recyclerview.widget.StaggeredGridLayoutManager#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: PostsActivity.java    From Ruisi with Apache License 2.0 6 votes vote down vote up
@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.menu:
            if (getType() == PostListAdapter.TYPE_IMAGE) {
                StaggeredGridLayoutManager m = (StaggeredGridLayoutManager) mLayoutManager;
                int span = m.getSpanCount();
                if (span == 1) {
                    m.setSpanCount(2);
                } else {
                    m.setSpanCount(1);
                }
            } else {
                if (isLogin()) {
                    Intent i = new Intent(this, NewPostActivity.class);
                    i.putExtra(NewPostActivity.FID, fid);
                    i.putExtra(NewPostActivity.TITLE, title);
                    startActivityForResult(i, 0);
                }
            }
            break;
        default:
            break;
    }
}
 
Example 2
Source File: FamiliarRecyclerView.java    From FamiliarRecyclerView with MIT License 6 votes vote down vote up
public int getFirstVisiblePosition() {
    LayoutManager layoutManager = getLayoutManager();

    if (null == layoutManager) return 0;

    int ret = -1;

    switch (mLayoutManagerType) {
        case LAYOUT_MANAGER_TYPE_LINEAR:
            ret = ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition() - getHeaderViewsCount();
            break;
        case LAYOUT_MANAGER_TYPE_GRID:
            ret = ((GridLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition() - getHeaderViewsCount();
            break;
        case LAYOUT_MANAGER_TYPE_STAGGERED_GRID:
            StaggeredGridLayoutManager tempStaggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            int[] firstVisibleItemPositions = new int[tempStaggeredGridLayoutManager.getSpanCount()];
            tempStaggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(firstVisibleItemPositions);
            ret = firstVisibleItemPositions[0] - getHeaderViewsCount();
            break;
    }

    return ret < 0 ? 0 : ret;
}
 
Example 3
Source File: EasyRefreshLayout.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
/**
 * 最后一个的位置
 */
private int getLastVisiBleItem() {
    RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
    int lastVisibleItemPosition = -1;

    int layoutManagerType = 0;
    if (layoutManager instanceof GridLayoutManager) {
        layoutManagerType = 1;
    } else if (layoutManager instanceof LinearLayoutManager) {
        layoutManagerType = 0;
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        layoutManagerType = 2;
    } else {
        throw new RuntimeException(
                "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
    }

    switch (layoutManagerType) {
        case 0:
            lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case 1:
            lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case 2:
            StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            int[] lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
            staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
            lastVisibleItemPosition = findMax(lastPositions);
            break;
    }
    return lastVisibleItemPosition;
}
 
Example 4
Source File: HorizontalScrollCompat.java    From SmoothRefreshLayout with MIT License 5 votes vote down vote up
public static boolean canAutoLoadMore(View view) {
    if (ViewCatcherUtil.isRecyclerView(view)) {
        RecyclerView recyclerView = (RecyclerView) view;
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager == null) {
            return false;
        }
        int lastVisiblePosition = 0;
        if (manager instanceof LinearLayoutManager) {
            LinearLayoutManager linearManager = ((LinearLayoutManager) manager);
            if (linearManager.getOrientation() != RecyclerView.HORIZONTAL) {
                return false;
            }
            lastVisiblePosition = linearManager.findLastVisibleItemPosition();
        } else if (manager instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager gridLayoutManager = (StaggeredGridLayoutManager) manager;
            if (gridLayoutManager.getOrientation() != RecyclerView.HORIZONTAL) {
                return false;
            }
            int[] lastPositions = new int[gridLayoutManager.getSpanCount()];
            gridLayoutManager.findLastVisibleItemPositions(lastPositions);
            for (int value : lastPositions) {
                if (value > lastVisiblePosition) {
                    lastVisiblePosition = value;
                }
            }
        }
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        return adapter != null
                && adapter.getItemCount() > 0
                && lastVisiblePosition >= 0
                && lastVisiblePosition >= adapter.getItemCount() - 1;
    }
    return false;
}
 
Example 5
Source File: HorizontalScrollCompat.java    From SmoothRefreshLayout with MIT License 5 votes vote down vote up
public static boolean canAutoRefresh(View view) {
    if (ViewCatcherUtil.isRecyclerView(view)) {
        RecyclerView recyclerView = (RecyclerView) view;
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager == null) {
            return false;
        }
        int firstVisiblePosition = -1;
        if (manager instanceof LinearLayoutManager) {
            LinearLayoutManager linearManager = ((LinearLayoutManager) manager);
            if (linearManager.getOrientation() != RecyclerView.HORIZONTAL) {
                return false;
            }
            firstVisiblePosition = linearManager.findFirstVisibleItemPosition();
        } else if (manager instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager gridLayoutManager = (StaggeredGridLayoutManager) manager;
            if (gridLayoutManager.getOrientation() != RecyclerView.HORIZONTAL) {
                return false;
            }
            int[] firstPositions = new int[gridLayoutManager.getSpanCount()];
            gridLayoutManager.findFirstVisibleItemPositions(firstPositions);
            for (int value : firstPositions) {
                if (value == 0) {
                    firstVisiblePosition = 0;
                    break;
                }
            }
        }
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        return adapter != null && firstVisiblePosition == 0;
    }
    return false;
}
 
Example 6
Source File: LayoutManagerUtil.java    From RvHelper with Apache License 2.0 5 votes vote down vote up
private static int caseStaggeredGrid(RecyclerView.LayoutManager layoutManager, boolean findFirst) {
    StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
    int[] positions = new int[staggeredGridLayoutManager.getSpanCount()];

    if (findFirst) {
        staggeredGridLayoutManager.findFirstVisibleItemPositions(positions);
        Arrays.sort(positions);
        return positions[0];
    } else {
        staggeredGridLayoutManager.findLastVisibleItemPositions(positions);
        Arrays.sort(positions);
        return positions[positions.length - 1];
    }
}
 
Example 7
Source File: FamiliarDefaultItemDecoration.java    From FamiliarRecyclerView with MIT License 5 votes vote down vote up
private void initLayoutManagerType() {
    mLayoutManagerType = mFamiliarRecyclerView.getCurLayoutManagerType();
    RecyclerView.LayoutManager layoutManager = mFamiliarRecyclerView.getLayoutManager();
    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:
            mLayoutManagerType = FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_LINEAR;
    }

    initDivisible();
}
 
Example 8
Source File: FamiliarRecyclerView.java    From FamiliarRecyclerView with MIT License 5 votes vote down vote up
public int getLastVisiblePosition() {
    LayoutManager layoutManager = getLayoutManager();
    if (null == layoutManager) return -1;

    int curItemCount = (null != mReqAdapter ? mReqAdapter.getItemCount() - 1 : 0);
    int ret = -1;

    switch (mLayoutManagerType) {
        case LAYOUT_MANAGER_TYPE_LINEAR:
            ret = ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition() - getHeaderViewsCount();
            if (ret > curItemCount) {
                ret -= getFooterViewsCount();
            }
            break;
        case LAYOUT_MANAGER_TYPE_GRID:
            ret = ((GridLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition() - getHeaderViewsCount();
            if (ret > curItemCount) {
                ret -= getFooterViewsCount();
            }
            break;
        case LAYOUT_MANAGER_TYPE_STAGGERED_GRID:
            StaggeredGridLayoutManager tempStaggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            int[] lastVisibleItemPositions = new int[tempStaggeredGridLayoutManager.getSpanCount()];
            tempStaggeredGridLayoutManager.findLastCompletelyVisibleItemPositions(lastVisibleItemPositions);
            if (lastVisibleItemPositions.length > 0) {
                int maxPos = lastVisibleItemPositions[0];
                for (int curPos : lastVisibleItemPositions) {
                    if (curPos > maxPos) maxPos = curPos;
                }
                ret = maxPos - getHeaderViewsCount();
                if (ret > curItemCount) {
                    ret -= getFooterViewsCount();
                }
            }
            break;
    }

    return ret < 0 ? (null != mReqAdapter ? mReqAdapter.getItemCount() - 1 : 0) : ret;
}
 
Example 9
Source File: FamiliarRecyclerViewOnScrollListener.java    From FamiliarRecyclerView with MIT License 5 votes vote down vote up
public FamiliarRecyclerViewOnScrollListener(RecyclerView.LayoutManager layoutManager) {
    if (layoutManager.getClass().isAssignableFrom(LinearLayoutManager.class)) {
        mLayoutManagerType = 1;
        mLinearLayoutManager = (LinearLayoutManager) layoutManager;
    } else if (layoutManager.getClass().isAssignableFrom(GridLayoutManager.class)) {
        mLayoutManagerType = 2;
        mGridLayoutManager = (GridLayoutManager) layoutManager;
    } else if (layoutManager.getClass().isAssignableFrom(StaggeredGridLayoutManager.class)) {
        mLayoutManagerType = 3;
        mStaggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
        mStaggeredLastPositions = new int[mStaggeredGridLayoutManager.getSpanCount()];
        mStaggeredFirstPositions = new int[mStaggeredGridLayoutManager.getSpanCount()];
    }
}
 
Example 10
Source File: EndlessRecyclerViewScrollListener.java    From adamant-android with GNU General Public License v3.0 4 votes vote down vote up
public EndlessRecyclerViewScrollListener(StaggeredGridLayoutManager layoutManager) {
    this.mLayoutManager = layoutManager;
    visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}
 
Example 11
Source File: EndlessUpScrollListener.java    From adamant-android with GNU General Public License v3.0 4 votes vote down vote up
public EndlessUpScrollListener(StaggeredGridLayoutManager layoutManager) {
    this.mLayoutManager = layoutManager;
    visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}
 
Example 12
Source File: EndlessRecyclerViewScrollListener.java    From grblcontroller with GNU General Public License v3.0 4 votes vote down vote up
public EndlessRecyclerViewScrollListener(StaggeredGridLayoutManager layoutManager) {
    this.mLayoutManager = layoutManager;
    visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}
 
Example 13
Source File: UltimateRecyclerView.java    From UltimateRecyclerView with Apache License 2.0 4 votes vote down vote up
private void scroll_load_more_detection(RecyclerView recyclerView) {

        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

        if (layoutManagerType == null) {
            if (layoutManager instanceof GridLayoutManager) {
                layoutManagerType = LAYOUT_MANAGER_TYPE.GRID;
            } else if (layoutManager instanceof StaggeredGridLayoutManager) {
                layoutManagerType = LAYOUT_MANAGER_TYPE.STAGGERED_GRID;
            } else if (layoutManager instanceof LinearLayoutManager) {
                layoutManagerType = LAYOUT_MANAGER_TYPE.LINEAR;
            } else {
                throw new RuntimeException("Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
            }
        }

        mTotalItemCount = layoutManager.getItemCount();
        mVisibleItemCount = layoutManager.getChildCount();

        switch (layoutManagerType) {
            case LINEAR:
                mFirstVisibleItem = mRecyclerViewHelper.findFirstVisibleItemPosition();
                lastVisibleItemPosition = mRecyclerViewHelper.findLastVisibleItemPosition();
                break;
            case GRID:
                if (layoutManager instanceof GridLayoutManager) {
                    GridLayoutManager ly = (GridLayoutManager) layoutManager;
                    lastVisibleItemPosition = ly.findLastVisibleItemPosition();
                    mFirstVisibleItem = ly.findFirstVisibleItemPosition();
                }
                break;
            case STAGGERED_GRID:
                if (layoutManager instanceof StaggeredGridLayoutManager) {
                    StaggeredGridLayoutManager sy = (StaggeredGridLayoutManager) layoutManager;

                    if (mlastPositionsStaggeredGridLayout == null)
                        mlastPositionsStaggeredGridLayout = new int[sy.getSpanCount()];

                    sy.findLastVisibleItemPositions(mlastPositionsStaggeredGridLayout);
                    lastVisibleItemPosition = findMax(mlastPositionsStaggeredGridLayout);

                    sy.findFirstVisibleItemPositions(mlastPositionsStaggeredGridLayout);
                    mFirstVisibleItem = findMin(mlastPositionsStaggeredGridLayout);
                }
                break;
        }

        if (automaticLoadMoreEnabled) {

            if (mTotalItemCount > previousTotal) {
                automaticLoadMoreEnabled = false;
                previousTotal = mTotalItemCount;
            }
        }

        boolean bottomEdgeHit = (mTotalItemCount - mVisibleItemCount) <= mFirstVisibleItem;

        if (bottomEdgeHit) {
            if (mIsLoadMoreWidgetEnabled) {
                /**auto activate load more**/
                if (!automaticLoadMoreEnabled) {
                    onLoadMoreListener.loadMore(mRecyclerView.getAdapter().getItemCount(), lastVisibleItemPosition);
                    automaticLoadMoreEnabled = true;
                }
            }
            mAdapter.internalExecuteLoadingView();
            previousTotal = mTotalItemCount;
        }
    }
 
Example 14
Source File: EndlessRecyclerViewScrollListener.java    From android-app with GNU General Public License v3.0 4 votes vote down vote up
public EndlessRecyclerViewScrollListener(StaggeredGridLayoutManager layoutManager) {
    this.mLayoutManager = layoutManager;
    visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}