![](/img/trans.png)
[英]Using RecyclerView with GridLayoutManager to add dynamic buttons to a layout
[英]RecyclerView GridLayoutManager: Individual layout per item count
您可以通過子類化GridLayoutManager
動態設置跨度,如下所示:
class CustomGridLayoutManager(context: Context) : GridLayoutManager(context, 2) {
override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) {
updateSpanCount()
super.onLayoutChildren(recycler, state)
}
private fun updateSpanCount() {
val colCount = if (childCount <= 4) {
2
} else {
3
}
this.spanCount = colCount
}
}
如果您想將元素居中放在最后一行,則通過GridLayoutManager
進行操作會有些GridLayoutManager
。 為此,您可能需要檢查FlexboxLayout 。 這是一個由Google自己開發的庫,具有在HTML5 / CSS世界中常見的flex box模型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.