[英]GridLayoutManager recalculate number of columns
我需要做這樣的事情:
我該怎么做?
更新
這幾乎是有效的。
GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup()
{
@Override
public int getSpanSize(int position)
{
if (position == mElements.size()-1)
{
return 1;
}
else
{
SchedulePlanGlobal elem = mElements.get(position);
SchedulePlanGlobal elemNext = mElements.get(position+1);
if (elem.getRoomName().charAt(0) == elemNext.getRoomName().charAt(0))
{
return 1;
}
else
{
return calculateSpan(position);
}
}
}
private int calculateSpan(int position)
{
if (position == 0) return 3;
else
{
int currentPosition = position - 1;
int elements = 0;
while (currentPosition != 0)
{
if (mElements.get(currentPosition).getRoomName().charAt(0) == mElements.get(position).getRoomName().charAt(0))
{
elements ++;
}
else
{
break;
}
currentPosition --;
}
int spanPosition = (elements % 3) + 1;
switch (spanPosition)
{
case 1: return 3;
case 2: return 2;
default: return 1;
}
}
}
});
實現並設置GridLayoutManager.SpanSizeLookup
。
override fun getSpanSize(position: Int) = /* set value based on position */
有了它,您可以定義應該使用多少跨度(列)項目。 在您的情況下,第 4 項應占用所有 3 列,其他項應僅占用 1 列。
不同的布局管理器可能是一個更簡單的解決方案。
我建議使用https://github.com/google/flexbox-layout
您可以將布局管理器設置為wrap
,因此如果您的項目不適合水平線,那么它會將其換行到新行,這樣可以輕松適應不同的屏幕尺寸。
然后,對於您知道要在新行上顯示的第 5 個項目,您將使用layout_wrapBefore
屬性。 我會在文檔中layout_wrapBefore
示例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.