【问题标题】:How to reduce space between columns in recycler view(GridLayoutManager) in android如何减少android中回收器视图(GridLayoutManager)中列之间的空间
【发布时间】:2016-03-28 06:28:07
【问题描述】:

我正在使用 recyclerview 的 GridLayoutManager 在我的应用程序中显示自定义图库。我已经实现了画廊等所有功能。但是我坚持了一件小事。在一行中,我有 3 张图像。但我需要减少图像之间的空间。这样做时,我不想连续显示超过 3 张图像,但图像大小(如果需要)可以增加。

【问题讨论】:

  • 谢谢,我试过这个,但是第一个imageview的顶部没有空格。并在最后一个图像视图中添加了额外的空间。水平和垂直间距也不均匀。
  • 在这里添加图片,现在是什么,你想要什么?

标签: android android-layout android-recyclerview gridlayoutmanager


【解决方案1】:

您可以使用自定义RecyclerViewItemDecorator:

public class RecyclerViewItemDecorator extends RecyclerView.ItemDecoration {
  private int spaceInPixels;

  public RecyclerViewItemDecorator(int spaceInPixels) {
    this.spaceInPixels = spaceInPixels;
  }

  @Override
  public void getItemOffsets(Rect outRect, View view, 
      RecyclerView parent, RecyclerView.State state) {
    outRect.left = spaceInPixels;
    outRect.right = spaceInPixels;
    outRect.bottom = spaceInPixels;

    if (parent.getChildLayoutPosition(view) == 0) {
        outRect.top = spaceInPixels;
    } else {
        outRect.top = 0;
    }
  }
}

然后将其添加到您的RecyclerView:

// For example 10 pixels
int spaceInPixels = 10;
mRecyclerView.addItemDecoration(new RecyclerViewItemDecorator(spaceInPixels));

希望对你有帮助!

【讨论】:

    【解决方案2】:

    将此用于回收站视图,

        this.mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        this.mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getContext())
                .color(Color.DKGRAY)
                .build());
        this.mRecyclerView.addItemDecoration(new VerticalDividerItemDecoration.Builder(getContext())
                .color(Color.DKGRAY)
                .build());
    

    在每个项目的布局文件中,将图像设置为符合容器边缘的方式。

    例如:

    <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/RlayoutGrid"
      android:layout_width="220dp"
      android:layout_height="170dp"
      android:background="@android:color/black">
    
    
        <ImageView
          android:id="@+id/prefence_imageButton"
          android:layout_width="match_parent"
          android:layout_height="170dp"
          android:background="@android:color/black"
          android:focusable="false"
          android:focusableInTouchMode="false"
          android:scaleType="fitXY"
          />
    
    </RelativeLayout>
    

    并将其添加到您的 build.gradle

     compile 'com.yqritc:recyclerview-flexibledivider:1.2.4'
    

    【讨论】:

    • 它说,无法解析 Horizo​​ntalDividerItemDecoration.Builder 和 VerticalDividerItemDecoration.Builder
    • @DAgrawal 您忘记添加依赖项。是的,我是 necroposter。
    【解决方案3】:

    要删除间距,您可以使用outRect.setEmpty()

    RecyclerView.ItemDecoration divider = new RecyclerView.ItemDecoration(){
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.setEmpty();
        }
    };
    grid.addItemDecoration(divider);
    

    【讨论】:

      猜你喜欢
      • 2011-11-17
      • 2021-06-30
      • 1970-01-01
      • 2013-08-03
      • 2021-08-19
      • 1970-01-01
      • 2010-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多