在RecyclerView的末尾添加空间

Hal*_*iaz 36 android android-layout android-gridlayout android-recyclerview

我有一个带有gridlayout的recyclerview.我想要的是当用户滚动到列表的末尾时(请参阅我的坏模型),应该有一个高度为50dp的空白空间,这与我的网格尺寸不同.

请注意,此空间仅在最末端可见,因为我不想更改布局.我可以这样做,以便recycerview的边距为50dp,但我不想这样做.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:scrollbars="vertical"
        />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

mbm*_*bmc 131

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

  • android:clipToPadding ="false"是必须的. (16认同)
  • 你还需要添加```android:scrollbarStyle ="outsideOverlay"```以便滚动条一直到底部.否则它会停在底部之上,看起来很黑 (9认同)
  • 为什么投票失败?此代码完全符合OP要求:仅在到达列表末尾时在底部添加空格.投票前请检查代码或提供解释. (6认同)
  • 我猜您被否决了,因为它还会占用回收者视图底部的空间。他需要在底部留出空间,但滚动内容应从底部移出时,填充会使其从上方50dp处出现。我不是下选民:P (2认同)

Ada*_*m S 40

最好用物品装饰来实现.

这是一个适用于的示例LinearLayoutManager- 您必须调整以适应您的网格布局.它做的是检查每个项目以查看它是否是最后一项,如果是,则将偏移量添加到它的底部.对于网格布局,困难的部分是确定您的项目位置是否在最后一行.

// After setting layout manager, adapter, etc...
float offsetPx = getResources().getDimension(R.dimen.bottom_offset_dp);
BottomOffsetDecoration bottomOffsetDecoration = new BottomOffsetDecoration((int) offsetPx);
mRecyclerView.addItemDecoration(bottomOffsetDecoration);

...

static class BottomOffsetDecoration extends RecyclerView.ItemDecoration {
    private int mBottomOffset;

    public BottomOffsetDecoration(int bottomOffset) {
        mBottomOffset = bottomOffset;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int dataSize = state.getItemCount();
        int position = parent.getChildAdapterPosition(view);
        if (dataSize > 0 && position == dataSize - 1) {
            outRect.set(0, 0, 0, mBottomOffset);
        } else {
            outRect.set(0, 0, 0, 0);
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

对于a GridLayoutManager,在getItemOffsets方法内部,您可以执行与此类似的操作,以确定它是否是最后一行:

GridLayoutManager grid = (GridLayoutManager)parent.getLayoutManager();
if ((dataSize - position) <= grid.getSpanCount()) {
    outRect.set(0, 0, 0, mBottomOffset);
} else {
    outRect.set(0, 0, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)

  • 此解决方案仅在项目数不变的情况下有效.当一个新项目添加到最后时,新的和最后一个项目将具有该底部间距. (4认同)

us_*_*vid 6

我有类似的问题。在阅读了所有其他人的回复后,我发现 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)

有关之前和之后的效果,请参阅 androidblog.us 上的链接: 将空间添加到 Android Recylerview 的末尾
让我知道它是如何为您工作的。

大卫