android - RecyclerView 自动适配 LayoutManager

标签 android android-layout android-recyclerview

我正在使用 RecyclerViewStaggeredGridLayoutManager

我希望这个 StaggeredGridLayoutManager 或任何一个 LayoutManager 占据空白区域(如果有的话)。例如,如果我设置 spanCount=3 即使我有 2 个项目或 1 个项目,它也必须占据所有屏幕宽度。在 StaggeredGridLayoutManager 中,我可以通过以下方式完全跨越单行:setFullSpan(true); 但不能仅在一行中跨越 2 个项目。

我的 RecyclerView 代码:

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

我试过了 AsymmetricGridViewtwoway-view但总有一 block 空地

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

enter image description here

这是 Google Keep 应用程序,您可以看到每一行的行高都是固定的,但项目的宽度是灵活的,我从未见过 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? 有人可以建议我任何方式或任何想法或指导我到我必须开始的地方吗?

最佳答案

/**
 * 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();
}

关于android - RecyclerView 自动适配 LayoutManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39756256/

相关文章:

javascript - 移动滚动条固定元素底部透明开口

android - 如何使复选框在 android 中看起来更好

java - 如何通过点击生成新 Activity

android - SearchView AutoComplete 出现黑色边框并覆盖搜索文本?

机器人:重叠图像?

java - 从 recyclerview 项目中将选定的 RadioButton 的文本保存在模型类中

android - 再次显示工具栏和底部导航 View

android - RecyclerView - 再次绑定(bind) View 持有者

android - 小米、魅族、华为的闹钟管理器

php - 使用 Android 应用程序在本地测试 PHP Web 服务