我正在使用 LiveData 从数据库中获取 30 个项目,如下所示:
@Query("SELECT * FROM table ORDER BY id DESC LIMIT 30")
fun getLast30(): LiveData<List<Person>>
每个回收站视图项都有一个点赞按钮,并且我在适配器中有一个接口:
interface OnItemClickedAdapter {
fun onFavButtonClicked(position: Int)
}
当点击like按钮时结束我正在更新数据库中的单个项目。当我使用 LiveData 时,它正在获取更新的 30 个项目。我正在使用观察方法设置该数据:
viewModel.last30.observe(viewLifecycleOwner, {
adapter.submitList(it)
}
问题:
- 当单击一个赞按钮时,回收器视图正在刷新其所有内容并滚动到第一个项目。
编辑: submitList(list: List) 方法:
fun submitList(persons: List<Person>) {
val diffResult: DiffUtil.DiffResult = DiffUtil.calculateDiff(
ApodGalleryAdapterDiffCallback(
this.persons,
persons
)
)
this.persons = person
diffResult.dispatchUpdatesTo(this)
}
DiffUtill 类:
class PersonAdapterDiffCallback(var oldList: List<Person>, var newList: List<Person>) :
DiffUtil.Callback() {
override fun getOldListSize(): Int {
return oldList.size
}
override fun getNewListSize(): Int {
return newList.size
}
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return oldList[oldItemPosition].url == newList[newItemPosition].url
}
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return oldList[oldItemPosition] == newList[newItemPosition]
}
}
适配器类:
class ApodGalleryAdapter() :
RecyclerView.Adapter<ApodGalleryAdapter.ApodGalleryViewHolder>() {
var onItemClickedGalleryAdapter: OnItemClickedGalleryAdapter? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ApodGalleryViewHolder {
return ApodGalleryViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.item_apod_gallery, parent, false),
onItemClickedGalleryAdapter
)
}
private var apods: List<Apod> = ArrayList()
override fun getItemCount(): Int {
return apods.size
}
override fun onBindViewHolder(holder: ApodGalleryViewHolder, position: Int) {
holder.bindData(apods[position], position)
}
fun submitList(apods: List<Apod>) {
val diffResult: DiffUtil.DiffResult = DiffUtil.calculateDiff(
ApodGalleryAdapterDiffCallback(
this.apods,
apods
)
)
this.apods = apods
diffResult.dispatchUpdatesTo(this)
}
class ApodGalleryViewHolder(
itemView: View,
var onItemClickedGalleryAdapter: OnItemClickedGalleryAdapter?
) :
RecyclerView.ViewHolder(itemView) {
private val apodImage: ImageView = itemView.image_item_apod
private val apodTitle: TextView = itemView.title_item_apod
private val apodDescription: TextView = itemView.description_item_apod
private val apodCopyright: TextView = itemView.copyright_item_apod
fun bindData(apod: Apod, position: Int) {
when {
apod.url.endsWith(".jpg") -> {
Glide.with(apodImage.context)
.load(apod.url)
.placeholder(R.drawable.transparent)
.into(apodImage)
}
apod.url.contains("youtube") -> {
Glide.with(apodImage.context)
.load(getThumbnailUrl(apod.url))
.placeholder(R.drawable.transparent)
.into(apodImage)
}
else -> {
apodImage.setImageResource(R.drawable.transparent)
}
}
apodTitle.text = apod.title
apodDescription.text = apod.explanation
apodCopyright.text = apod.copyright
if (apod.isLiked) {
itemView.fav_button_item_apod_gallery.setImageResource(R.drawable.ic_round_favorite_24)
} else {
itemView.fav_button_item_apod_gallery.setImageResource(R.drawable.ic_round_favorite_border_24)
}
itemView.setOnClickListener {
onItemClickedGalleryAdapter?.onClick(apod.date)
}
itemView.fav_button_item_apod_gallery.setOnClickListener {
onItemClickedGalleryAdapter?.onFavButtonClicked(position)
}
}
}
}
interface OnItemClickedGalleryAdapter {
fun onClick(date: String)
fun onFavButtonClicked(position: Int)
}
class ApodGalleryAdapterDiffCallback(var oldList: List<Apod>, var newList: List<Apod>) :
DiffUtil.Callback() {
override fun getOldListSize(): Int {
return oldList.size
}
override fun getNewListSize(): Int {
return newList.size
}
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return oldList[oldItemPosition].url == newList[newItemPosition].url
}
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return oldList[oldItemPosition] == newList[newItemPosition]
}
}
我已经覆盖了equals()
我的 Data 类的方法:
override fun equals(other: Any?): Boolean {
if (javaClass != other?.javaClass)
return false
other as Apod
if (id != other.id)
return false
if (copyright != other.copyright)
return false
if (date != other.date)
return false
if (explanation != other.explanation)
return false
if (hdUrl != other.hdUrl)
return false
if (media_type != other.media_type)
return false
if (service_version != other.service_version)
return false
if (url != other.url)
return false
if (isLiked != other.isLiked)
return false
return true
}