添加了界面及逻辑
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
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.sw.platecabinet.adapter
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Rect
|
||||
import android.view.View
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
|
||||
/**
|
||||
* 为RecyclerView添加间距的ItemDecoration
|
||||
*/
|
||||
class GridSpacingItemDecoration(
|
||||
private val spanCount: Int, // 列数
|
||||
private val spacing: Int, // 间距(dp)
|
||||
private val includeEdge: Boolean // 是否包含边缘
|
||||
) : RecyclerView.ItemDecoration() {
|
||||
|
||||
override fun getItemOffsets(
|
||||
outRect: Rect,
|
||||
view: View,
|
||||
parent: RecyclerView,
|
||||
state: RecyclerView.State
|
||||
) {
|
||||
val position = parent.getChildAdapterPosition(view) // item position
|
||||
val column = position % spanCount // item column
|
||||
|
||||
if (includeEdge) {
|
||||
// 左右间距计算
|
||||
outRect.left = spacing - column * spacing / spanCount
|
||||
outRect.right = (column + 1) * spacing / spanCount
|
||||
|
||||
// 第一行添加顶部间距
|
||||
if (position < spanCount) {
|
||||
outRect.top = spacing
|
||||
}
|
||||
outRect.bottom = spacing // 每行底部都添加间距
|
||||
} else {
|
||||
// 左右间距计算(不包含边缘)
|
||||
outRect.left = column * spacing / spanCount
|
||||
outRect.right = spacing - (column + 1) * spacing / spanCount
|
||||
|
||||
// 不是第一行时添加顶部间距
|
||||
if (position >= spanCount) {
|
||||
outRect.top = spacing
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将dp值转换为px
|
||||
*/
|
||||
fun Context.dpToPx(dp: Int): Int {
|
||||
return (dp * resources.displayMetrics.density).toInt()
|
||||
}
|
||||
Reference in New Issue
Block a user