RecyclerView自动调整LayoutManager

时间:2016-09-28 19:38:27

标签: android android-layout android-recyclerview

我正在使用RecyclerView StaggeredGridLayoutManager

我想要StaggeredGridLayoutManagerLayoutManager占用空白区域(如果有)。例如,如果我设置spanCount = 3 它必须占据所有屏幕宽度,即使我有2项或1项。在StaggeredGridLayoutManager我可以完全跨越单行:setFullSpan(true);但不能跨越两行只有一行。

我的RecyclerView代码:

StaggeredGridLayoutManager sglm= new StaggeredGridLayoutManager(spanCount,StaggeredGridLayoutManager.VERTICAL);
sglm.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);

我尝试了AsymmetricGridViewtwoway-view,但总是有一个空白区域

我收集了Facebook App的一些截图:

enter image description here

以下是Google Keep App,因为您可以看到每行的行高都是固定的,但项目的宽度是灵活的,我从未见过Google Keep的任何空白区域:

enter image description here

当我使用它时,总会有一个空白区域,因为你可以看到图像的黑色部分是空的。我希望RecyclerView应该通过扩展我的行来占用该区域,就像在Google Keep应用中一样:

enter image description here

我访问了此页面:android-how-to-create-a-facebook-like-images-gallery-grid但它没有帮助我。

以下是我访问的另一个页面:grid-of-images-like-facebook-for-android

是否有人使用RecyclerView或任何View这样的人? 有人可以建议我任何方式或任何想法或指导我到我必须开始的地方吗?

3 个答案:

答案 0 :(得分:3)

/**
 * A LayoutManager that lays out children in a staggered grid formation.
 * It supports horizontal & vertical layout as well as an ability to layout children in reverse.
 * <p>
 * Staggered grids are likely to have gaps at the edges of the layout. To avoid these gaps,
 * StaggeredGridLayoutManager can offset spans independently or move items between spans. You can
 * control this behavior via {@link #setGapStrategy(int)}.
 */


/**
 * Sets the gap handling strategy for StaggeredGridLayoutManager. If the gapStrategy parameter
 * is different than the current strategy, calling this method will trigger a layout request.
 *
 * @param gapStrategy The new gap handling strategy. Should be
 *                    {@link #GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS} or {@link
 *                    #GAP_HANDLING_NONE}.
 * @see #getGapStrategy()
 */
public void setGapStrategy(int gapStrategy) {
    assertNotInLayoutOrScroll(null);
    if (gapStrategy == mGapStrategy) {
        return;
    }
    if (gapStrategy != GAP_HANDLING_NONE &&
            gapStrategy != GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS) {
        throw new IllegalArgumentException("invalid gap strategy. Must be GAP_HANDLING_NONE "
                + "or GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS");
    }
    mGapStrategy = gapStrategy;
    setAutoMeasureEnabled(mGapStrategy != GAP_HANDLING_NONE);
    requestLayout();
}

答案 1 :(得分:3)

在Git上查看

BitFrames

它可以帮助您设置您想要的图像。

在Play商店中查看

PlayStore App

答案 2 :(得分:0)

我最近在FlexboxLayoutManager看到了FlexBox Layout Github

如果您想获得与Google Keep应用程序相同的效果,则可以使用它。