RecyclerVIew 中使用 GridLayoutManger 的装饰间距和跨度
Posted
技术标签:
【中文标题】RecyclerVIew 中使用 GridLayoutManger 的装饰间距和跨度【英文标题】:Decoration spacing and span with GridLayoutManger in RecyclerVIew 【发布时间】:2021-08-09 07:40:44 【问题描述】:我正在尝试通过使用 GridLayoutMangaer 作为其布局来实现这种 RecyclerView。但是,我在为网格中的每个项目实现项目间距时遇到了这个问题。
我的目标是实现如下图所示
Please click on this link to preview image
任何此类实施的解决方案将不胜感激
【问题讨论】:
【参考方案1】: public class GridRec extends RecyclerView.ItemDecoration
private int space;
public GridRec(int space)
this.space = space;
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state)
outRect.left = space;
outRect.right = space;
outRect.bottom = space;
if (parent.getChildLayoutPosition(view) == 0)
outRect.top = space;
else
outRect.top = 0;
int my_space = getResources().getDimension(R.dimen.test);
myRecyclerView.addItemDecoration(new GridRec(my_space));
-----------------------------------------------------------
public class abc extends RecyclerView.ItemDecoration
private int myPadding_psx;
private int myPadding_edge_psx;
public abc(Activity activity)
final Resources resources = activity.getResources();
myPadding_psx = (int) resources.getDimension(R.dimen.myDefault);
myPadding_edge_psx = (int) resources.getDimension(R.dimen.mYDefaultEdge);
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
super.getItemOffsets(outRect, view, parent, state);
final int itemPosition = parent.getChildAdapterPosition(view);
if (itemPosition == RecyclerView.NO_POSITION)
return;
int orientation = getOrientation(parent);
final int itemCount = state.getItemCount();
int left = 0;
int top = 0;
int right = 0;
int bottom = 0;
//Horizontal
if (orientation == LinearLayoutManager.HORIZONTAL)
left = myPadding_psx;
right =myPadding_psx;
if (itemPosition == 0)
left += myPadding_edge_psx;
else if (itemCount > 0 && itemPosition == itemCount - 1)
right += myPadding_edge_psx;
//Vertical
else
top = myPadding_psx;
bottom = myPadding_psx;
if (itemPosition == 0)
top += myPadding_edge_psx;
else if (itemCount > 0 && itemPosition == itemCount - 1)
bottom += myPadding_edge_psx;
if (!isReverseLayout(parent))
outRect.set(left, top, right, bottom);
else
outRect.set(right, bottom, left, top);
private boolean isReverseLayout(RecyclerView parent)
if (parent.getLayoutManager() instanceof LinearLayoutManager)
LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
return layoutManager.getReverseLayout();
else
throw new IllegalStateException("Error.");
private int getOrientation(RecyclerView parent)
if (parent.getLayoutManager() instanceof LinearLayoutManager)
LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
return layoutManager.getOrientation();
else
throw new IllegalStateException("Error.");
【讨论】:
感谢您的代码片段。但是通过实现这一点,左边的开始和右边的结束与中间的空间不同,如图中所示。您是否有任何想法将左侧开始和右侧结束调整为与中间相同? 请试试上面更新的那个。希望它有效 我已应用您更新的代码。它没有给左右空间。只为每个项目提供顶部和底部空间。对于这种情况,我使用 GridLayoutManger 和 spanCount 3 然后你可以在布局资源文件中设置你的adapter(onbindviewholder)项的padding或margin。以上是关于RecyclerVIew 中使用 GridLayoutManger 的装饰间距和跨度的主要内容,如果未能解决你的问题,请参考以下文章
RecyclerView | 在 RecyclerView 中使用 ListAdapter
RecyclerView | 在 RecyclerView 中使用 header 快人一步
Eclipse中使用recyclerview时出现Caused by: java.lang.NoClassDefFoundError: android.support.v7.recyclerview.