java - 交错网格布局管理器 : Adding Margin makes the layout items pushed to the side

标签 java android android-layout android-recyclerview staggered-gridview

所以,我正在开发一个小项目,我想将 StaggeredGridLayoutManager 添加到文件中。如果我在布局中不添加 itemDecorator 并且没有 marginpadding ,它就会起作用。一旦我添加边距,它就会将列表推到一侧。我附上屏幕截图来澄清问题。 enter image description here



这是我的代码:

recyclerView.setHasFixedSize(true);
    StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    recyclerView.addItemDecoration(new EqualSpaceItemDecoration(8));
    recyclerView.setLayoutManager(gridLayoutManager);
    recyclerView.setAdapter(adapter);

以及该项目的布局文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical">
  <android.support.v7.widget.CardView
    android:id="@+id/wallpaper_item_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@color/cardview_light_background"
    app:cardCornerRadius="4dp"
    app:cardElevation="4dp"
    app:cardPreventCornerOverlap="false">

    <ImageView
      android:id="@+id/wallpaper_item_image"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

  </android.support.v7.widget.CardView>
</LinearLayout>

我已经环顾了一段时间,但无法找到解决问题的方法。任何帮助表示赞赏。谢谢。

最佳答案

我不知道您是否已经找到解决方案,但我会留下评论,以防其他人看到这个问题。

我通过执行以下操作解决了这个问题:

layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);

示例:

StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);          
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(layoutManager);

关于java - 交错网格布局管理器 : Adding Margin makes the layout items pushed to the side,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52289080/

相关文章:

java - Java 6 Mac OS X Lion 上的奇怪并发行为

java - 以编程方式锁定屏幕

android - 在 com.android.support :design:23. 1.0 中更改左侧、顶部、右侧或底部的 TabLayout 图标

java - 看不到通知 - Android Studio

java - 在 Android 中按下 ListView 中的内容时转到下一个布局

java - 使按钮不可点击,直到 TitleAreaDialog 中满足某些条件?

java - spring jdbc中如何使用数组

java - 在 JPA 中使用投影查询

android - 检查后复选框错误不会消失

android - Listview 和 TextView 对齐