pull/32/head
GKF 6 years ago
parent dc4a9df268
commit 317df0dd55
  1. 30
      app/src/main/java/io/legado/app/help/AdapterDataObserverProxy.kt
  2. 20
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookGroupAdapter.kt

@ -0,0 +1,30 @@
package io.legado.app.help
import androidx.recyclerview.widget.RecyclerView
internal class AdapterDataObserverProxy(var adapterDataObserver: RecyclerView.AdapterDataObserver, var headerCount: Int) : RecyclerView.AdapterDataObserver() {
override fun onChanged() {
adapterDataObserver.onChanged()
}
override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
adapterDataObserver.onItemRangeChanged(positionStart + headerCount, itemCount)
}
override fun onItemRangeChanged(positionStart: Int, itemCount: Int, payload: Any?) {
adapterDataObserver.onItemRangeChanged(positionStart + headerCount, itemCount, payload)
}
// 当第n个数据被获取,更新第n+1个position
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
adapterDataObserver.onItemRangeInserted(positionStart + headerCount, itemCount)
}
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
adapterDataObserver.onItemRangeRemoved(positionStart + headerCount, itemCount)
}
override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
super.onItemRangeMoved(fromPosition + headerCount, toPosition + headerCount, itemCount)
}
}

@ -8,6 +8,7 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import io.legado.app.R import io.legado.app.R
import io.legado.app.data.entities.BookGroup import io.legado.app.data.entities.BookGroup
import io.legado.app.help.AdapterDataObserverProxy
import kotlinx.android.synthetic.main.item_book_group.view.* import kotlinx.android.synthetic.main.item_book_group.view.*
class BookGroupAdapter : PagedListAdapter<BookGroup, BookGroupAdapter.MyViewHolder>(DIFF_CALLBACK) { class BookGroupAdapter : PagedListAdapter<BookGroup, BookGroupAdapter.MyViewHolder>(DIFF_CALLBACK) {
@ -26,12 +27,18 @@ class BookGroupAdapter : PagedListAdapter<BookGroup, BookGroupAdapter.MyViewHold
} }
var callBack: CallBack? = null var callBack: CallBack? = null
private val defaultGroups = arrayOf(BookGroup(-5, "全部"), private val defaultGroups = arrayOf(BookGroup(-1, "全部"),
BookGroup(-5, "本地"), BookGroup(-2, "本地"),
BookGroup(-5, "音频")) BookGroup(-3, "音频"))
private val addBookGroup = BookGroup(-10, "+")
override fun getItemCount(): Int { override fun getItemCount(): Int {
return super.getItemCount() + defaultGroups.size return super.getItemCount() + defaultGroups.size + 1
}
override fun registerAdapterDataObserver(observer: RecyclerView.AdapterDataObserver) {
super.registerAdapterDataObserver(AdapterDataObserverProxy(observer, defaultGroups.size))
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
@ -41,6 +48,8 @@ class BookGroupAdapter : PagedListAdapter<BookGroup, BookGroupAdapter.MyViewHold
override fun onBindViewHolder(holder: MyViewHolder, position: Int) { override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
if (position < defaultGroups.size) { if (position < defaultGroups.size) {
holder.bind(defaultGroups[position], callBack) holder.bind(defaultGroups[position], callBack)
} else if (position == itemCount - 1) {
holder.bind(addBookGroup, callBack)
} else { } else {
currentList?.get(position - defaultGroups.size)?.let { currentList?.get(position - defaultGroups.size)?.let {
holder.bind(it, callBack) holder.bind(it, callBack)
@ -52,10 +61,11 @@ class BookGroupAdapter : PagedListAdapter<BookGroup, BookGroupAdapter.MyViewHold
fun bind(bookGroup: BookGroup, callBack: CallBack?) = with(itemView) { fun bind(bookGroup: BookGroup, callBack: CallBack?) = with(itemView) {
tv_group.text = bookGroup.groupName tv_group.text = bookGroup.groupName
tv_group.setOnClickListener { callBack?.open(bookGroup.groupId) }
} }
} }
interface CallBack { interface CallBack {
fun open(groupId: Int)
} }
} }
Loading…
Cancel
Save