添加了界面及逻辑
This commit is contained in:
@@ -40,17 +40,16 @@ class GenericPageAdapter<T, VB : ViewBinding>(
|
||||
fun bind(items: List<T>) {
|
||||
childRecyclerView.layoutManager = GridLayoutManager(itemView.context, 2)
|
||||
|
||||
// 添加间距装饰(12dp)
|
||||
childRecyclerView.addItemDecoration(
|
||||
GridSpacingItemDecoration(
|
||||
spanCount = 2, // 2列
|
||||
spacing = itemView.context.dpToPx(12), // 12dp
|
||||
spacing = itemView.context.dpToPx(6), // 12dp
|
||||
includeEdge = true // 包含边缘间距
|
||||
)
|
||||
)
|
||||
|
||||
childRecyclerView.adapter =
|
||||
GenericChildAdapter(items, itemBindingInflater, itemBindCallback)
|
||||
GenericItemAdapter(items, itemBindingInflater, itemBindCallback)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,11 +57,11 @@ class GenericPageAdapter<T, VB : ViewBinding>(
|
||||
/**
|
||||
* 通用子项适配器(支持ViewBinding)
|
||||
*/
|
||||
class GenericChildAdapter<T, VB : ViewBinding>(
|
||||
private val items: List<T>,
|
||||
class GenericItemAdapter<T, VB : ViewBinding>(
|
||||
private var 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>() {
|
||||
) : RecyclerView.Adapter<GenericItemAdapter<T, VB>.ViewHolder>() {
|
||||
|
||||
inner class ViewHolder(private val binding: VB) : RecyclerView.ViewHolder(binding.root) {
|
||||
fun bind(item: T, position: Int) {
|
||||
@@ -70,6 +69,11 @@ class GenericChildAdapter<T, VB : ViewBinding>(
|
||||
}
|
||||
}
|
||||
|
||||
fun updateData(newItem: List<T>) {
|
||||
this.items = newItem
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val binding = bindingInflater(LayoutInflater.from(parent.context), parent, false)
|
||||
return ViewHolder(binding)
|
||||
@@ -77,7 +81,6 @@ class GenericChildAdapter<T, VB : ViewBinding>(
|
||||
|
||||
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