优化
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 {
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
|
||||||
val holder = ItemViewHolder(getViewBinding(parent))
|
return 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract fun getViewBinding(parent: ViewGroup): VB
|
protected abstract fun getViewBinding(parent: ViewGroup): VB
|
||||||
@@ -149,8 +128,29 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
|
|||||||
position: Int,
|
position: Int,
|
||||||
payloads: MutableList<Any>
|
payloads: MutableList<Any>
|
||||||
) {
|
) {
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
registerListener(holder, (holder.binding as VB))
|
||||||
|
registerItemListener(holder)
|
||||||
getItem(holder.layoutPosition)?.let {
|
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 -> {
|
else -> {
|
||||||
val holder = ItemViewHolder(getViewBinding(parent))
|
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -405,8 +384,29 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
|
|||||||
payloads: MutableList<Any>
|
payloads: MutableList<Any>
|
||||||
) {
|
) {
|
||||||
if (!isHeader(holder.layoutPosition) && !isFooter(holder.layoutPosition)) {
|
if (!isHeader(holder.layoutPosition) && !isFooter(holder.layoutPosition)) {
|
||||||
getItemByLayoutPosition(holder.layoutPosition)?.let {
|
@Suppress("UNCHECKED_CAST")
|
||||||
convert(holder, (holder.binding as VB), it, payloads)
|
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.recyclerview.widget.DiffUtil
|
||||||
import androidx.viewbinding.ViewBinding
|
import androidx.viewbinding.ViewBinding
|
||||||
import io.legado.app.base.adapter.DiffRecyclerAdapter
|
import io.legado.app.base.adapter.DiffRecyclerAdapter
|
||||||
|
import io.legado.app.base.adapter.ItemViewHolder
|
||||||
import io.legado.app.data.entities.Book
|
import io.legado.app.data.entities.Book
|
||||||
|
|
||||||
abstract class BaseBooksAdapter<VB : ViewBinding>(context: Context) :
|
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) {
|
fun notification(bookUrl: String) {
|
||||||
getItems().forEachIndexed { i, it ->
|
getItems().forEachIndexed { i, it ->
|
||||||
if (it.bookUrl == bookUrl) {
|
if (it.bookUrl == bookUrl) {
|
||||||
|
|||||||
@@ -17,8 +17,7 @@ class BooksAdapterList(
|
|||||||
context: Context,
|
context: Context,
|
||||||
private val callBack: CallBack,
|
private val callBack: CallBack,
|
||||||
private val lifecycle: Lifecycle
|
private val lifecycle: Lifecycle
|
||||||
) :
|
) : BaseBooksAdapter<ItemBookshelfListBinding>(context) {
|
||||||
BaseBooksAdapter<ItemBookshelfListBinding>(context) {
|
|
||||||
|
|
||||||
override fun getViewBinding(parent: ViewGroup): ItemBookshelfListBinding {
|
override fun getViewBinding(parent: ViewGroup): ItemBookshelfListBinding {
|
||||||
return ItemBookshelfListBinding.inflate(inflater, parent, false)
|
return ItemBookshelfListBinding.inflate(inflater, parent, false)
|
||||||
@@ -105,4 +104,4 @@ class BooksAdapterList(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user