As you said, setSpanLookup
is the best method, I think.
Try this code:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val layoutManager = GridLayoutManager(this, 2)
layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
if (position == 6 || position == 13) return 2
else return 1
}
}
rv.layoutManager = layoutManager
val items = mutableListOf<String>()
for (i in 1..14) {
items.add("item$i")
}
rv.adapter = MyAdapter(items)
}
}
You need to know the value getSpanSize
returned means weight, not column count.
In terms of item layout, you can use different ViewHolder in special position.
For example:
companion object{
const val NORMAL = 0
const val WIDE = 1
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
...
if (viewType == WIDE) return WideViewHolder(wideView)
else return NormalViewHolder(normalView)
}
override fun getItemViewType(position: Int): Int {
if (position == 6 || position == 13) {
return WIDE
}
return NORMAL
}