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)
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)
我有类似的问题。在阅读了所有其他人的回复后,我发现 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 的末尾
让我知道它是如何为您工作的。
大卫
| 归档时间: |
|
| 查看次数: |
11393 次 |
| 最近记录: |