pull/37/head
kunfei 5 years ago
parent 091d2508fd
commit 2a53b94754
  1. 33
      app/src/main/java/io/legado/app/ui/main/bookshelf/BooksDiffCallBack.kt
  2. 4
      app/src/main/java/io/legado/app/ui/main/bookshelf/BooksFragment.kt

@ -0,0 +1,33 @@
package io.legado.app.ui.main.bookshelf
import androidx.recyclerview.widget.DiffUtil
import io.legado.app.data.entities.Book
class BooksDiffCallBack(private val oldItems: List<Book>, private val newItems: List<Book>) :
DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return oldItems[oldItemPosition].bookUrl == newItems[newItemPosition].bookUrl
}
override fun getOldListSize(): Int {
return oldItems.size
}
override fun getNewListSize(): Int {
return newItems.size
}
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition]
return oldItem.name == newItem.name
&& oldItem.durChapterTitle == newItem.durChapterTitle
&& oldItem.latestChapterTitle == newItem.latestChapterTitle
&& oldItem.getDisplayCover() == newItem.getDisplayCover()
&& oldItem.getUnreadChapterNum() == newItem.getUnreadChapterNum()
&& oldItem.lastCheckCount == newItem.lastCheckCount
}
}

@ -5,6 +5,7 @@ import android.view.View
import androidx.core.content.ContextCompat
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.App
@ -86,7 +87,10 @@ class BooksFragment : VMBaseFragment<BooksViewModel>(R.layout.fragment_books),
bookshelfLiveData?.observe(
this,
Observer {
val diffResult =
DiffUtil.calculateDiff(BooksDiffCallBack(booksAdapter.getItems(), it))
booksAdapter.setItems(it)
diffResult.dispatchUpdatesTo(booksAdapter)
})
}

Loading…
Cancel
Save