用于更改行和列跨度的 RecyclerView 布局管理器 [英] RecyclerView layoutmanager for changing row and column span
问题描述
我希望创建以下布局(红色正方形不一定是正方形,自定义固定高度也可以).布局看起来很简单,但我没有找到适合我需求的布局管理器.
我尝试了 StaggeredGridLayoutManager 但这仅允许设置列或行的跨度,不能同时设置两者.
是否有一个能够做到这一点的自定义布局管理器?我看过 TwoWayView 但改变高度似乎有问题,图书馆没有得到维护.
有人遇到这个问题并解决了吗?
有点晚了,但可能对其他人有帮助...
我一直在寻找相同的东西,最后,经过 2 天的谷歌搜索,我找到了一个解决方案.非常感谢
解决方案很简单:
1.下载 SpannableGridLayoutManager
样式错误将这些行放入 attrs.xml
<attr name="android:orientation"/><attr name="spanCount"/><attr name="aspectRatio" format="string"/></declare-styleable>
I am looking to create the following layout (red sqaures does not really have to be squares, a custom fixed height is also fine). The layout seems very simple yet I did not find a layout manager which suits my needs.
I tried the StaggeredGridLayoutManager but this only allows to set the span of either the column or the row, not both.
Is there floating around a custom layout manager which is able to do this? I have looked at TwoWayView but changing height seems to be problematic and the library is not being maintained.
Anyone having this problem and solved it?
A little bit late, but it might help everyone else...
I was looking for the same and finally, after 2 days of googling I found a solution. Big thanks to Nick Butcher! His layout manager called SpannableGridLayoutManager
is capable of doing this. In my case I was doing a pattern like your first two rows:
Solution is easy:
1. Download SpannableGridLayoutManager from here
For some reason I had to change this line:
while (availableSpace > 0 && lastVisiblePosition < lastItemPosition) {
to
while (lastVisiblePosition < lastItemPosition) {
to got the manager working.
2. Set SpannableGridLayoutManger to your RecyclerView
In my case:
SpannedGridLayoutManager manager = new SpannedGridLayoutManager(
new SpannedGridLayoutManager.GridSpanLookup() {
@Override
public SpannedGridLayoutManager.SpanInfo getSpanInfo(int position) {
// Conditions for 2x2 items
if (position % 6 == 0 || position % 6 == 4) {
return new SpannedGridLayoutManager.SpanInfo(2, 2);
} else {
return new SpannedGridLayoutManager.SpanInfo(1, 1);
}
}
},
3, // number of columns
1f // how big is default item
);
Which gave me exactly what I wanted (numbers are position of item in adapter):
EDIT: error with styleable
Put those lines into attrs.xml
<declare-styleable name="SpannedGridLayoutManager">
<attr name="android:orientation" />
<attr name="spanCount" />
<attr name="aspectRatio" format="string" />
</declare-styleable>
这篇关于用于更改行和列跨度的 RecyclerView 布局管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!