Android使用gridlayoutmanager在recyclerview中的最后一个元素下添加间距

pra*_*tsJ 71 android gridlayoutmanager android-recyclerview

我试图在增加最后一个元件行下面的间距RecyclerViewGridLayoutManager.ItemDecoration为了这个目的,我使用自定义底部填充,当它的最后一个元素如下:

public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
private int space;
private int bottomSpace = 0;

public SpaceItemDecoration(int space, int bottomSpace) {
    this.space = space;
    this.bottomSpace = bottomSpace;
}

public SpaceItemDecoration(int space) {
    this.space = space;
    this.bottomSpace = 0;
}

@Override
public void getItemOffsets(Rect outRect, View view,
                           RecyclerView parent, RecyclerView.State state) {

    int childCount = parent.getChildCount();
    final int itemPosition = parent.getChildAdapterPosition(view);
    final int itemCount = state.getItemCount();

    outRect.left = space;
    outRect.right = space;
    outRect.bottom = space;
    outRect.top = space;

    if (itemCount > 0 && itemPosition == itemCount - 1) {
        outRect.bottom = bottomSpace;
    }
}
}
Run Code Online (Sandbox Code Playgroud)

但是这种方法的问题在于它在最后一行中弄乱了网格中的元素高度.我猜是GridLayoutManager根据左边的间距改变元素的高度.实现这一目标的正确方法是什么?

这将正常工作LinearLayoutManager.只是在遇到GridLayoutManager问题时.

它非常有用,如果你有一个FAB在底部并需要在最后一行滚动上面的项目,FAB以便它们可见.

ved*_*ant 367

只需添加填充和设置 android:clipToPadding="false"

<RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="8dp"
    android:clipToPadding="false" />
Run Code Online (Sandbox Code Playgroud)

感谢这个精彩的答案!

  • 这正是我想要的!谢谢! (3认同)
  • 这不会将回收器视图的滚动条扩展到最底部.编辑:防止这个添加`android:scrollbarStyle ="outsideOverlay"` (3认同)
  • 这样做对我来说.当您需要在回收商中平均分配物品时,快速简便的解决方案.谢谢. (2认同)

小智 11

这个问题的解决方案在于重写GridLayoutManager的SpanSizeLookup.

您必须在活动或片段中对要扩充RecylerView的GridlayoutManager进行更改.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //your code 
    recyclerView.addItemDecoration(new PhotoGridMarginDecoration(context));

    // SPAN_COUNT is the number of columns in the Grid View
    GridLayoutManager gridLayoutManager = new GridLayoutManager(context, SPAN_COUNT);

    // With the help of this method you can set span for every type of view
    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            if (list.get(position).getType() == TYPE_HEADER) {
                // Will consume the whole width
                return gridLayoutManager.getSpanCount();
            } else if (list.get(position).getType() == TYPE_CONTENT) {
                // will consume only one part of the SPAN_COUNT
                return 1;
            } else if(list.get(position).getType() == TYPE_FOOTER) {
                // Will consume the whole width
                // Will take care of spaces to be left,
                // if the number of views in a row is not equal to 4
                return gridLayoutManager.getSpanCount();
            }
            return gridLayoutManager.getSpanCount();
        }
    });
    recyclerView.setLayoutManager(gridLayoutManager);
}
Run Code Online (Sandbox Code Playgroud)


小智 9

仅在最后一项的情况下,应在 Recycler View 中使用装饰作为底部边距

recyclerView.addItemDecoration(MemberItemDecoration())

public class MemberItemDecoration extends RecyclerView.ItemDecoration {

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        // only for the last one
        if (parent.getChildAdapterPosition(view) == parent.getAdapter().getItemCount() - 1) {
            outRect.bottom = 50/* set your margin here */;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


us_*_*vid 8

我有类似的问题并回复了堆栈溢出中的另一个线程。为了帮助登陆此页面的其他人,我将在此处重新发布。

在阅读了所有其他人的回复后,我发现 recyclerview 的布局 xml 中的更改按预期对我的回收站视图起作用:

android:paddingBottom="127px"
android:clipToPadding="false"
android:scrollbarStyle="outsideOverlay"  
Run Code Online (Sandbox Code Playgroud)

完整的布局如下所示:

<android.support.v7.widget.RecyclerView
        android:id="@+id/library_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="160px"
        android:layout_marginEnd="160px"
        tools:listitem="@layout/library_list_item" />  
Run Code Online (Sandbox Code Playgroud)