添加了界面及逻辑
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package com.sw.platecabinet.adapter
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.viewbinding.ViewBinding
|
||||
import com.sw.platecabinet.R
|
||||
|
||||
/**
|
||||
* 通用分页适配器(支持ViewBinding)
|
||||
*/
|
||||
class GenericPageAdapter<T, VB : ViewBinding>(
|
||||
private var pages: List<List<T>>,
|
||||
private val pageLayoutId: Int,
|
||||
private val itemBindingInflater: (LayoutInflater, ViewGroup, Boolean) -> VB,
|
||||
private val itemBindCallback: VB.(item: T, position: Int) -> Unit
|
||||
) : RecyclerView.Adapter<GenericPageAdapter<T, VB>.PageViewHolder>() {
|
||||
|
||||
fun updatePages(newPages: List<List<T>>) {
|
||||
this.pages = newPages
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PageViewHolder {
|
||||
val view = LayoutInflater.from(parent.context).inflate(pageLayoutId, parent, false)
|
||||
return PageViewHolder(view)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: PageViewHolder, position: Int) {
|
||||
holder.bind(pages[position])
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = pages.size
|
||||
|
||||
inner class PageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
private val childRecyclerView: RecyclerView = itemView.findViewById(R.id.childRecyclerView)
|
||||
|
||||
fun bind(items: List<T>) {
|
||||
childRecyclerView.layoutManager = GridLayoutManager(itemView.context, 2)
|
||||
|
||||
// 添加间距装饰(12dp)
|
||||
childRecyclerView.addItemDecoration(
|
||||
GridSpacingItemDecoration(
|
||||
spanCount = 2, // 2列
|
||||
spacing = itemView.context.dpToPx(12), // 12dp
|
||||
includeEdge = true // 包含边缘间距
|
||||
)
|
||||
)
|
||||
|
||||
childRecyclerView.adapter =
|
||||
GenericChildAdapter(items, itemBindingInflater, itemBindCallback)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用子项适配器(支持ViewBinding)
|
||||
*/
|
||||
class GenericChildAdapter<T, VB : ViewBinding>(
|
||||
private val items: List<T>,
|
||||
private val bindingInflater: (LayoutInflater, ViewGroup, Boolean) -> VB,
|
||||
private val bindCallback: VB.(item: T, position: Int) -> Unit,
|
||||
) : RecyclerView.Adapter<GenericChildAdapter<T, VB>.ViewHolder>() {
|
||||
|
||||
inner class ViewHolder(private val binding: VB) : RecyclerView.ViewHolder(binding.root) {
|
||||
fun bind(item: T, position: Int) {
|
||||
binding.bindCallback(item, position)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val binding = bindingInflater(LayoutInflater.from(parent.context), parent, false)
|
||||
return ViewHolder(binding)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
holder.bind(items[position], position)
|
||||
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = items.size
|
||||
}
|
||||
Reference in New Issue
Block a user