mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -111,28 +111,7 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
|
||||
val holder = ItemViewHolder(getViewBinding(parent))
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
registerListener(holder, (holder.binding as VB))
|
||||
|
||||
if (itemClickListener != null) {
|
||||
holder.itemView.setOnClickListener {
|
||||
getItem(holder.layoutPosition)?.let {
|
||||
itemClickListener?.invoke(holder, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (itemLongClickListener != null) {
|
||||
holder.itemView.onLongClick {
|
||||
getItem(holder.layoutPosition)?.let {
|
||||
itemLongClickListener?.invoke(holder, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return holder
|
||||
return ItemViewHolder(getViewBinding(parent))
|
||||
}
|
||||
|
||||
protected abstract fun getViewBinding(parent: ViewGroup): VB
|
||||
@@ -149,8 +128,29 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
|
||||
position: Int,
|
||||
payloads: MutableList<Any>
|
||||
) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
registerListener(holder, (holder.binding as VB))
|
||||
registerItemListener(holder)
|
||||
getItem(holder.layoutPosition)?.let {
|
||||
convert(holder, (holder.binding as VB), it, payloads)
|
||||
convert(holder, holder.binding, it, payloads)
|
||||
}
|
||||
}
|
||||
|
||||
private fun registerItemListener(holder: ItemViewHolder) {
|
||||
if (itemClickListener != null) {
|
||||
holder.itemView.setOnClickListener {
|
||||
getItem(holder.layoutPosition)?.let {
|
||||
itemClickListener?.invoke(holder, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (itemLongClickListener != null) {
|
||||
holder.itemView.onLongClick {
|
||||
getItem(holder.layoutPosition)?.let {
|
||||
itemLongClickListener?.invoke(holder, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -369,28 +369,7 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
|
||||
}
|
||||
|
||||
else -> {
|
||||
val holder = ItemViewHolder(getViewBinding(parent))
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
registerListener(holder, (holder.binding as VB))
|
||||
|
||||
if (itemClickListener != null) {
|
||||
holder.itemView.setOnClickListener {
|
||||
getItemByLayoutPosition(holder.layoutPosition)?.let {
|
||||
itemClickListener?.invoke(holder, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (itemLongClickListener != null) {
|
||||
holder.itemView.onLongClick {
|
||||
getItemByLayoutPosition(holder.layoutPosition)?.let {
|
||||
itemLongClickListener?.invoke(holder, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
holder
|
||||
ItemViewHolder(getViewBinding(parent))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -405,8 +384,29 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
|
||||
payloads: MutableList<Any>
|
||||
) {
|
||||
if (!isHeader(holder.layoutPosition) && !isFooter(holder.layoutPosition)) {
|
||||
getItemByLayoutPosition(holder.layoutPosition)?.let {
|
||||
convert(holder, (holder.binding as VB), it, payloads)
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
registerListener(holder, (holder.binding as VB))
|
||||
registerItemListener(holder)
|
||||
getItemByLayoutPosition(holder.layoutPosition)?.let { item ->
|
||||
convert(holder, holder.binding, item, payloads)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun registerItemListener(holder: ItemViewHolder) {
|
||||
if (itemClickListener != null) {
|
||||
holder.itemView.setOnClickListener {
|
||||
getItemByLayoutPosition(holder.layoutPosition)?.let {
|
||||
itemClickListener?.invoke(holder, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (itemLongClickListener != null) {
|
||||
holder.itemView.onLongClick {
|
||||
getItemByLayoutPosition(holder.layoutPosition)?.let {
|
||||
itemLongClickListener?.invoke(holder, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import androidx.core.os.bundleOf
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.viewbinding.ViewBinding
|
||||
import io.legado.app.base.adapter.DiffRecyclerAdapter
|
||||
import io.legado.app.base.adapter.ItemViewHolder
|
||||
import io.legado.app.data.entities.Book
|
||||
|
||||
abstract class BaseBooksAdapter<VB : ViewBinding>(context: Context) :
|
||||
@@ -65,6 +66,12 @@ abstract class BaseBooksAdapter<VB : ViewBinding>(context: Context) :
|
||||
|
||||
}
|
||||
|
||||
override fun onViewRecycled(holder: ItemViewHolder) {
|
||||
super.onViewRecycled(holder)
|
||||
holder.itemView.setOnClickListener(null)
|
||||
holder.itemView.setOnLongClickListener(null)
|
||||
}
|
||||
|
||||
fun notification(bookUrl: String) {
|
||||
getItems().forEachIndexed { i, it ->
|
||||
if (it.bookUrl == bookUrl) {
|
||||
|
||||
@@ -17,8 +17,7 @@ class BooksAdapterList(
|
||||
context: Context,
|
||||
private val callBack: CallBack,
|
||||
private val lifecycle: Lifecycle
|
||||
) :
|
||||
BaseBooksAdapter<ItemBookshelfListBinding>(context) {
|
||||
) : BaseBooksAdapter<ItemBookshelfListBinding>(context) {
|
||||
|
||||
override fun getViewBinding(parent: ViewGroup): ItemBookshelfListBinding {
|
||||
return ItemBookshelfListBinding.inflate(inflater, parent, false)
|
||||
@@ -105,4 +104,4 @@ class BooksAdapterList(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user