// ---------------用法-----------------------
// 准备数据
// list.add(PAdapter2Mode(0, "").setCon(1))
//
// list.add(PAdapter2Mode(1, "").setCon(1))
//
// list.add(PAdapter2Mode(2, "").setCon(1))
//设置给view
// binding.rec.layoutManager = GridLayoutManager(this, 6)
// val adapter = PAdapter2Kt(
// list, { binding, position, type, data ->
// when (type) {
// 0 -> {
// var binding1 = binding.binding as item1Binding
//
// }
//
// 1 -> {
// var binding2 = binding.binding as item2Binding
//
// }
// }
//
// },
// R.layout.item1,// 自己的不同类型的item 布局
// R.layout.item2,
// R.layout.item3
// )
//
// binding.rec.adapter = adapter
package com.and.myapplication
import androidx.recyclerview.widget.RecyclerView
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import androidx.databinding.DataBindingUtil
import android.view.LayoutInflater
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
import java.util.ArrayList
import java.io.Serializable
import android.view.View
open class PAdapter2Kt(
mData: List,
var bindViewInterface: (binding: ListHolder, position: Int, type: Int, data: Any) -> Unit,
vararg layoutId: Int
) : RecyclerView.Adapter() {
private val layoutId: IntArray
private var list: List = ArrayList()
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getItemViewType(position: Int): Int {
return list[position].type
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListHolder {
val binding = DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
layoutId[viewType],
parent,
false
)
val listHolder = ListHolder(binding.root)
listHolder.binding = binding
return listHolder
}
override fun onBindViewHolder(holder: ListHolder, position: Int) {
bindViewInterface(
holder,
position,
getItemViewType(position),
if (list[position].data == null) "" else list[position].data!!
)
}
override fun getItemCount(): Int {
return list.size
}
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
val manager = recyclerView.layoutManager
if (manager is GridLayoutManager) {
val gridManager = manager
gridManager.spanSizeLookup = object : SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return if (list[position].con == 0) gridManager.spanCount else list[position].con
}
}
}
}
init {
list = mData
this.layoutId = layoutId
this.bindViewInterface = bindViewInterface
}
}
class ListHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
var binding: ViewDataBinding? = null
}
class PAdapter2Mode : Serializable {
var type: Int
var gid = 0
fun Setgid(gid: Int): PAdapter2Mode {
this.gid = gid
return this
}
var con = 0
fun Setcon(con: Int): PAdapter2Mode {
this.con = con
return this
}
var data: Any? = null
private set
fun setData(data: Any?): PAdapter2Mode {
this.data = data
return this
}
constructor(type: Int, data: Any?) {
this.type = type
this.data = data
}
fun setCon(con: Int): PAdapter2Mode {
this.con = con
return this
}
constructor(type: Int) {
this.type = type
}
fun setGid(gid: Int): PAdapter2Mode {
this.gid = gid
return this
}
}