/** * 0 代码 布局标签 必须 从0开始整数自增 比如1230 321504 如果tape 最大数字是2 就必须给adapter 设置至少三个布局 因为所有的不同种类的布局 都放在数组 然后通过下标获取 data 可是是任何类型 .setCon(1) 代表占几格 如果GridLayoutManager 的spanCount =6 .setCon(1) 就是占屏幕的1/6 .setCon(6) 就是宽度 全屏 */ // ---------------用法----------------------- // 准备数据 // 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 /** * 多布局 Adapter * 老规矩 先写用法 * 备注: 必须使用databinding 也就是说 item的layout 必须用标签包起来 不会的自行百度哦 * * * * * * * * */ 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 } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)