【发布时间】:2019-02-02 14:00:36
【问题描述】:
我正在使用 Room 和 Paging 库来显示类别。
我的实体:
@Entity(tableName = Database.Table.CATEGORIES)
data class Category(
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = ID) var id: Long = 0,
@ColumnInfo(name = NAME) var name: String = "",
@ColumnInfo(name = ICON_ID) var iconId: Int = 0,
@ColumnInfo(name = COLOR) @ColorInt var color: Int = DEFAULT_COLOR
)
我的 DAO:
@Query("SELECT * FROM $CATEGORIES")
fun getPagedCategories(): DataSource.Factory<Int, Category>
@Update
fun update(category: Category)
我的回购:
val pagedCategoriesList: LiveData<PagedList<Category>> = categoryDao.getPagedCategories().toLiveData(Config(CATEGORIES_LIST_PAGE_SIZE))
我的视图模型:
val pagedCategoriesList: LiveData<PagedList<Category>>
get() = repository.pagedCategoriesList
我的适配器:
class CategoriesAdapter(val context: Context) : PagedListAdapter<Category, CategoriesAdapter.CategoryViewHolder>(CategoriesDiffCallback()) {
//region Adapter
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder {
return CategoryViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_category, parent, false))
}
override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) {
holder.bind(getItem(position)!!)
}
//endregion
//region Methods
fun getItemAt(position: Int): Category = getItem(position)!!
//endregion
inner class CategoryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val iconHelper = IconHelper.getInstance(context)
fun bind(category: Category) {
with(itemView) {
txvCategoryItemText.text = category.name
imvCategoryItemIcon.setBackgroundColor(category.color)
iconHelper.addLoadCallback {
imvCategoryItemIcon.setImageDrawable(iconHelper.getIcon(category.iconId).getDrawable(context))
}
}
}
}
class CategoriesDiffCallback : DiffUtil.ItemCallback<Category>() {
override fun areItemsTheSame(oldItem: Category, newItem: Category): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: Category, newItem: Category): Boolean {
return oldItem == newItem
}
}
}
还有我的片段:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
categoryViewModel = ViewModelProviders.of(this).get(CategoryViewModel::class.java)
adapter = CategoriesAdapter(requireContext())
categoryViewModel.pagedCategoriesList.observe(this, Observer(adapter::submitList))
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ViewCompat.setTooltipText(fabNewCategory, getString(R.string.NewCategory))
with(mRecyclerView) {
layoutManager = GridLayoutManager(requireContext(), 4)
itemAnimator = DefaultItemAnimator()
addItemDecoration(SpacesItemDecoration(resources.getDimensionPixelSize(R.dimen.card_default_spacing)))
addOnItemTouchListener(OnItemTouchListener(requireContext(), this, this@CategoriesFragment))
}
mRecyclerView.adapter = adapter
fabNewCategory.setOnClickListener(this)
}
在插入、删除或加载类别时一切正常。 但是当我更新单个实体的颜色或文本时,列表没有更新,尽管提交列表被正确调用。
我调试了整个过程,发现了问题:
提交列表后,调用AsyncPagedListDiffer#submitList。我比较了之前的列表(AsyncPagedListDiffer 中的mPagedList)和新列表(AsyncPagedListDiffer#submitList 中的pagedList)。我在那里编辑的项目是相同的,并且已经保存了新数据。所以DiffUtil 比较所有内容,尽管显示的列表没有更新,但项目已经相等。
如果列表是参考,它会解释为什么适配器列表中的数据已经刷新,但是我该如何解决这个问题呢?
【问题讨论】:
-
您能分享一下您的
CategoriesAdapter实现吗?你是如何实现DiffUtil.ItemCallback的? -
@SanlokLee 查看我的编辑
标签: android kotlin android-recyclerview android-adapter android-paging