RecyclerViews support the concept of ItemDecoration: special offsets and drawing around each element. As seen in this answer, you can use
Then add it via
RecyclerViews support the concept of ItemDecoration: special offsets and drawing around each element. As seen in this answer, you can use
Then add it via
网友:@zatziky——是的,如果你已经在使用顶部和底部填充作为你的一部分(使用),然后你可以稍微重组的事情。然而,如果你不你只是移动如果检查最后一次(就像你仍然希望底部填充最后一项)。
(原文:@zatziky - yep, if you already use top and bottom padding as part of your RecyclerView
(and useclipToPadding
="false"
), then you can restructure things slightly. If you don't however, you'd just be moving the if check to be the last time (as you'd still want the bott
om padding on the last item).)
网友:我试着这个,除非我添加了android:clipToPadding=“false”和android:填充=“@dimen/间距”RecyclerView外面的间距不符合项之间的间距。我已经试了所有下面的答案,认为@yqritc是最干净,最简单的解决方案。
(原文:I tried this and unless I added android:clipToPadding="false" and android:padding="@dimen/spacing" to the RecyclerView the outside spacing did not match the spacing between items. I have tried all the answers below and think the solution by @yqritc was the cleanest and simplest.)
网友:@ianhanniballake,而这是在使用单孔布局管理器,这对多跨布局管理器失败。
(原文:@ianhanniballake, while this works when using a single span layout manager, it fails for multi-span layout manager.)
网友:值得提及,你必须修改outRect的所有字段。否则你可以抵消之前的观点。
(原文:It worth to mention, that you have to modify all fields of outRect. otherwise you can get the offset of the previous view.)
Following code works well, and each column has same width:
To use it:
1) no edge
2) with edge
(原文:thanks, It works great!)
网友:工作除非你有项目,有各种各样的跨越,像标题。
(原文:Works unless you have items that have various spans, like headers.)
网友:工作就像一个魅力!
(原文:Worked like a charm!)
网友:似乎只有在垂直模式工作
(原文:Seems to only work for vertical mode)
The following is the step-by-step simple solution if you want the equal spacing around items and equal item sizes.
In your source code, add ItemOffsetDecoration to your recyclerview.
Item offset value should be half size of the actual value you want to add as space between items.
Also, set item offset value as padding for its recyclerview, and specify android:clipToPadding=false.
DONE.
(原文:Simple and elegant)
网友:最佳答案——伟大的工作!
(原文:Best answer here - great job!)
网友:最佳答案-m8:感谢)
(原文:best answer- thanks m8 :))
网友:完美!就像一个魅力。
(原文:perfect! Works like a charm.)
网友:好先生!!
(原文:Well done sir!!)
Try this. It'll take care of equal spacing all around. Works both with List, Grid, and StaggeredGrid.
The updated code should handle most of the corner cases with spans, orientation, etc. Note that if using setSpanSizeLookup() with GridLayoutManager, setting setSpanIndexCacheEnabled() is recommended for performance reasons.
Note, it seems that with StaggeredGrid, there's seems to be a bug where the index of the children gets wacky and hard to track so the code below might not work very well with StaggeredGridLayoutManager.
Hope it helps.
(原文:thnx for this answer)
网友:这应该是验证答案。谢谢你!
(原文:This should be the verified answer. Thank you.)
网友:我有双跨后的第一道物品。这是因为parent.getChildCount第一项()返回1,2第二等等。所以,我建议添加空间项目的前边缘:outRect。顶级=childIndex<spancount吗?spacinginpixels:0,和每个条目的添加底空间:outrect。底=spacinginpixels;
(原文:I've got double span just after first line of items. It happens because parent.getChildCount() returns 1 for first item, 2 for second and so on. So, I suggest add space to items of the top edge like: outRect.top = childIndex < spanCount ? spacingInPixels : 0; And add bottom space for each item: outRect.bottom = spacingInPixels;)
网友:在滚动的时候RecyclerView,间距变了。
(原文:At the time of scrolling RecyclerView, spacing changed.)
网友:我认为parent.getChildCount()应该改为“parent.getLayoutManager().getItemCount()”。同时,isBottomEdge功能需要改变“回归childIndex>=childCount-spanCount+spanIndex”。改变这些之后,我得到了相等的间距。但是请注意,这个解决方案不给我等于项目大小如果跨数大于2因为偏移值取决于位置是不同的。
(原文:I think parent.getChildCount() should be changed to "parent.getLayoutManager().getItemCount()". Also, isBottomEdge function need to be changed to "return childIndex >= childCount - spanCount + spanIndex". After changing these, I got equal spacing. But please note that this solution does not give me equal item sizes if span count is greater than 2 since offset value is different depending on position.)
The following code will handle StaggeredGridLayoutManager, GridLayoutManager, and LinearLayoutManager.
Then use it
(原文:This is the most simple one. One important thing is you also got to add the padding to the parent in the xml. In my case, it work that way. Thanks.)
网友:实际上增加了填充到父(回收商的观点)。
(原文:The SpaceItemDecoration
actually adds the padding to the parent (the recycler view).)
网友:只填充出现(右侧)当我没有设置填充到父在xml中
(原文:only halfSpace
padding appeared(to the right side) when I had not set the padding to the parent in xml)
网友:只是缺失的右边?也许你有半空间设置为leftPadding左边已经在xml,这段代码只剩下检查填充设置RecyclerView与否。
(原文:It was only missing on the right side? It may be that you have half space set as the leftPadding on the left side already in the xml and this code only checks if the left padding is set on the RecyclerView or not.)
网友:这是最简洁的解决方案!
(原文:this is the most concise solution!)
Answers above have clarified ways to set margin handling GridLayoutManager and LinearLayoutManager.
But for StaggeredGridLayoutManager, Pirdad Sakhizada's answer say it "might not work very well with StaggeredGridLayoutManager." It should be the problem about indexOfSpan.
You can get it by this way: