Android:SVG Drawables停止工作或在RecyclerView列表中出现故障

问题描述 投票:0回答:1

我遇到了一个问题,我导入了一些SVG可绘制对象(在Illustrator中进行了优化,并且具有短路径数据-因此它们的复杂性不在讨论之列),并在RecyclerView项目中显示它们。问题在于,在多次测试应用程序之后,它们将停止工作或开始出现毛刺(例如缺少块或形状)。足够奇怪的是,应用程序缓存擦除可以解决问题,并且它们可以正常工作,直到我从Android Studio中运行该应用程序约5至6次为止。

这里是“停止工作”的意思:enter image description here

enter image description here

在一个活动中,它们显示为红色警告,在另一活动中,它们显示为指纹图标(因此我在整个项目中都没有这样的图标,也没有指纹实现。)>

这里是实现:

我像这样在会议室数据库中添加条目:

Category(icon = R.drawable.ic_category_homepage)

类别数据类如下所示:

@Entity(tableName = "categories")
data class Category(
  val title: String,
  @DrawableRes
  val icon: Int
)

因此,我在本地存储中将SVG可绘制引用添加为DrawableRes Int。然后,当我在适配器中显示图标时,我使用Glide:

        Glide.with(context)
          .load(category.icon)
          .transition(DrawableTransitionOptions.withCrossFade())
          .into(itemView.categoryIV)

这里是整个适配器:

class DrawerAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

private val categories: ArrayList<Category> = ArrayList()

fun submitCategories(newFeed: List<Category>, lifecycleCoroutineScope: LifecycleCoroutineScope) {
    lifecycleCoroutineScope.launch {
        val result = coroutineRunOnComputationThread {
            val oldFeed = categories
            val result: DiffUtil.DiffResult = DiffUtil.calculateDiff(
                DrawerDiffCallback(oldFeed, newFeed)
            )
            categories.clear()
            categories.addAll(newFeed)
            result
        }
        coroutineRunOnMainThread {
            result.dispatchUpdatesTo(this@DrawerAdapter)
        }
    }
}

override fun getItemCount(): Int = categories.size
override fun getItemId(position: Int): Long {
    return if (categories.isNullOrEmpty()) 0 else categories[position].id
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
    return DrawerItemViewHolder(parent.inflate(R.layout.item_drawer_menu))
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) =
    (holder as DrawerItemViewHolder).bind(categories[position])

inner class DrawerItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    fun bind(category: Category) = with(itemView) {
        Glide.with(context)
            .load(category.icon)
            .transition(DrawableTransitionOptions.withCrossFade())
            .into(itemDrawerIVIcon)

        if (category.preConfigured && category.resTitle != null)
            itemDrawerTVTitle.text = context.resources.getString(category.resTitle)
        else
            itemDrawerTVTitle.text = category.title
    }
}

private inner class DrawerDiffCallback(
    private var oldFeed: List<Category>,
    private var newFeed: List<Category>
) : DiffUtil.Callback() {
    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        val oldItem = oldFeed[oldItemPosition]
        val newItem = newFeed[newItemPosition]
        return oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        val oldItem = oldFeed[oldItemPosition]
        val newItem = newFeed[newItemPosition]
        return oldItem == newItem
    }

    override fun getOldListSize(): Int = oldFeed.size
    override fun getNewListSize(): Int = newFeed.size
}

}

知道为什么我会出现这种奇怪的行为吗?

[我遇到了一些导入SVG可绘制对象的问题(在Illustrator中进行了优化并且具有短路径数据-因此它们的复杂性已不在讨论之列,并在RecyclerView中显示它们...

android svg android-recyclerview drawable glide
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.