This commit is contained in:
kunfei
2023-02-27 22:02:58 +08:00
parent dad592546b
commit 1a99668a75
@@ -39,68 +39,33 @@ class BooksAdapterGrid(context: Context, callBack: CallBack) :
val bundle = payloads.getOrNull(0) as? Bundle
when {
bundle == null -> super.onBindViewHolder(holder, position, payloads)
holder is BookViewHolder -> onBindBook(holder.binding, position, bundle)
holder is GroupViewHolder -> onBindGroup(holder.binding, position, bundle)
}
}
private fun onBindGroup(
binding: ItemBookshelfGridGroupBinding,
position: Int,
@Suppress("UNUSED_PARAMETER") bundle: Bundle
) {
binding.run {
val item = callBack.getItem(position) as BookGroup
tvName.text = item.groupName
ivCover.load(item.cover)
}
}
private fun onBindBook(binding: ItemBookshelfGridBinding, position: Int, bundle: Bundle) {
binding.run {
val item = callBack.getItem(position) as? Book ?: return
bundle.keySet().forEach {
when (it) {
"name" -> tvName.text = item.name
"cover" -> ivCover.load(item.getDisplayCover(), item.name, item.author, false, item.origin)
"refresh" -> upRefresh(this, item)
holder is BookViewHolder -> (callBack.getItem(position) as? Book)?.let {
holder.onBind(it, bundle)
}
holder is GroupViewHolder -> (callBack.getItem(position) as? BookGroup)?.let {
holder.onBind(it, position)
}
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is BookViewHolder -> onBindBook(holder.binding, position)
is GroupViewHolder -> onBindGroup(holder.binding, position)
is BookViewHolder -> (callBack.getItem(position) as? Book)?.let {
holder.onBind(it, position)
}
}
private fun onBindGroup(binding: ItemBookshelfGridGroupBinding, position: Int) {
binding.run {
val item = callBack.getItem(position)
if (item is BookGroup) {
tvName.text = item.groupName
ivCover.load(item.cover)
}
root.setOnClickListener {
callBack.onItemClick(position)
}
root.onLongClick {
callBack.onItemLongClick(position)
is GroupViewHolder -> (callBack.getItem(position) as? BookGroup)?.let {
holder.onBind(it, position)
}
}
}
private fun onBindBook(binding: ItemBookshelfGridBinding, position: Int) {
binding.run {
val item = callBack.getItem(position)
if (item is Book) {
inner class BookViewHolder(val binding: ItemBookshelfGridBinding) :
RecyclerView.ViewHolder(binding.root) {
fun onBind(item: Book, position: Int) = binding.run{
tvName.text = item.name
ivCover.load(item.getDisplayCover(), item.name, item.author, false, item.origin)
upRefresh(this, item)
}
root.setOnClickListener {
callBack.onItemClick(position)
}
@@ -108,6 +73,15 @@ class BooksAdapterGrid(context: Context, callBack: CallBack) :
callBack.onItemLongClick(position)
}
}
fun onBind(item: Book, bundle: Bundle) = binding.run {
bundle.keySet().forEach {
when (it) {
"name" -> tvName.text = item.name
"cover" -> ivCover.load(item.getDisplayCover(), item.name, item.author, false, item.origin)
"refresh" -> upRefresh(this, item)
}
}
}
private fun upRefresh(binding: ItemBookshelfGridBinding, item: Book) {
@@ -125,10 +99,22 @@ class BooksAdapterGrid(context: Context, callBack: CallBack) :
}
}
class BookViewHolder(val binding: ItemBookshelfGridBinding) :
RecyclerView.ViewHolder(binding.root)
}
class GroupViewHolder(val binding: ItemBookshelfGridGroupBinding) :
RecyclerView.ViewHolder(binding.root)
inner class GroupViewHolder(val binding: ItemBookshelfGridGroupBinding) :
RecyclerView.ViewHolder(binding.root) {
fun onBind(item: BookGroup, position: Int) = binding.run {
tvName.text = item.groupName
ivCover.load(item.cover)
root.setOnClickListener {
callBack.onItemClick(position)
}
root.onLongClick {
callBack.onItemLongClick(position)
}
}
}
}