我有一个带有gridlayout的recyclerview。我想要的是当用户滚动到列表末尾(see my bad mockup)时,应该有一个空格,高度为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>
答案 0 :(得分:115)
<RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="50dp"
android:clipToPadding="false"
/>
答案 1 :(得分:37)
最好使用item decoration。
这是一个与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);
}
}
}
对于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);
}
答案 2 :(得分:2)
我有类似的问题。阅读所有其他答复后,我发现recyclerview布局xml中的更改按预期可用于我的recycler视图:
android:paddingBottom="127px"
android:clipToPadding="false"
android:scrollbarStyle="outsideOverlay"
完整的布局如下:
<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" />
有关之前和之后的效果,请参见androidblog.us上的链接:
Adding Space to End of Android Recylerview
让我知道它如何为您工作。
大卫
答案 3 :(得分:0)
您可以尝试以下代码,记住&#34;我不测试此代码&#34;
public class MyRecyclerView extends RecyclerView {
private Context context;
public MyRecyclerView(Context context) {
super(context);
this.context = context;
}
public MyRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public MyRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
View view = (View) getChildAt(getChildCount()-1);
int diff = (view.getBottom()-(getHeight()+getScrollY()+view.getTop()));
if( diff == 0 ){ // if diff is zero, then the bottom has been reached
TextView tv = new TextView(context);
tv.setHeight(dpToPx(50));
addView(tv,getChildCount());//update --> add to last
requestLayout();
}
super.onScrollChanged(l, t, oldl, oldt);
}
public int dpToPx(int dp) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return px;
}
}
并在布局中:
<your_packagae.MyRecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
/>
答案 4 :(得分:0)
使用BottomOffsetDecoration创建一个类名
public 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);
}
}
}
然后在将adapter和layoutmanager添加到recyclerview后添加这些行
float offsetPx = getResources().getDimension(R.dimen.bottom_offset_dp);
BottomOffsetDecoration bottomOffsetDecoration = new BottomOffsetDecoration((int) offsetPx);
rv.addItemDecoration(bottomOffsetDecoration);
并为 GridLayout 在分配 recyclerview 布局管理器后添加这些行
GridLayoutManager grid = (GridLayoutManager)parent.getLayoutManager();
if ((dataSize - position) <= grid.getSpanCount()) {
outRect.set(0, 0, 0, mBottomOffset);
} else {
outRect.set(0, 0, 0, 0);
}