gpt4 book ai didi

android - 如何在具有嵌套 RecyclerViews 的多 View 适配器上正确使用 RecycledViewPool?

转载 作者:行者123 更新时间:2023-12-04 23:50:55 25 4
gpt4 key购买 nike

我的应用上下文,以便您理解我的问题:

I am currently coding a very complex application with a huge dashboardthat contains a huge list of nested RecyclerViews (both horizontal andvertical). I know that the vertical RV inside a vertical RV is a badpractice but unfortunately here the management and client request isstronger than our advices and they really want it implemented. How did we get to vertical RVs in vertical RV? Our backend offers basically an arrayList of groups that need to be rendered inside our dashboard. Basically a server-driven-ui. These groups contain all sorts of different designs, vertical sub-lists, horizontal sub-lists etc. I have managed to build a prototype for this DashBoard adapter so far and now I want to dive deeper inside the performance optimizations.


我试图提高性能的事情:

I have implemented DiffUtil to make the dashboard update moreefficiently on a data refresh. I have set the adapter to havestableIDs using and unique hash that every group has. I have createdflat UI with constraintLayout with absolute minimum of layoutspossible. I have used data binding that was proved to actually improvethe performance just a little, not only better clean code. I am notdoing absolutely any data processing in the adapter or viewHolders,the entire data flow comes with a final shape from higher data layers.I have set linearLayoutManager.initialPrefetchItemCount = x on everynested RVs where x is the number of items rendered on screen beforeany scroll.


然后我遇到了 RecycledViewPool。我见过一些人用这个技巧提高列表性能的例子。基本上,他们获得了父大适配器的 recyclerViewPool 并将其设置在嵌套的 RV 上。我也实现了这一点,我的大问题是现在我所有的嵌套项目都完全混淆了。
而不是像这样拥有三个水平房车:
enter image description here
现在我把它们都混在一起了:
enter image description here
这是我的recycledViewPool 实现:
class GlobalAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

private val groups: ArrayList<Group> = ArrayList()

private var recyclerViewPool = RecyclerView.RecycledViewPool()

[...]

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
CATEGORIES_LIST.typeText.hashCode() -> {
val holder = CategoryGroupViewHolder(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.group_item_categories_list, parent, false
), adapterCallback
)
holder.binding.recyclerView.setRecycledViewPool(recyclerViewPool)
holder
}

[...]
所以我基本上将 viewPool 设置为 onCreateViewHolder 中的嵌套 RV。最重要的是,在这篇文章之后: https://medium.com/@thagikura/reduce-the-number-of-inflation-of-viewholders-drastically-by-sharing-a-viewpool-across-multiple-249d5fc6d28我还在查看器中做了以下事情:
class CategoryGroupViewHolder(
val binding: GroupItemCategoriesListBinding,
private val adapterCallback: adapterCallback?
):
RecyclerView.ViewHolder(binding.root) {

private val categoryAdapter = CategoryAdapter()
private val defaultItemAnimator = DefaultItemAnimator()
private val linearLayoutManager = LinearLayoutManager(
binding.root.context, LinearLayoutManager.HORIZONTAL, false
)

fun bind(group: Group) = with(binding.root) {
binding.categoryGroup = group
binding.executePendingBindings()
linearLayoutManager.initialPrefetchItemCount = 5
binding.recyclerView.apply {
adapter = categoryAdapter
linearLayoutManager.recycleChildrenOnDetach = true
layoutManager = linearLayoutManager
itemAnimator = defaultItemAnimator
}
}
通知 linearLayoutManager.recycleChildrenOnDetach = true .我在这里做错了什么,为什么我的项目在整个适配器中完全混合在一起。关于如何正确使用此 recycledViewPool 的任何建议?
另外,如果你们中的任何人对如何优化垂直 recyclerView 中的垂直 recyclerView 有一些建议,那将非常感激。谢谢

最佳答案

如果我没记错的话,我认为在这里使用 ConcatAdapter 会以更简单的方式解决您的问题。如果你想找一个例子,你可以看看我的 repo。
https://github.com/cnrture/ConcatAdapterExample

关于android - 如何在具有嵌套 RecyclerViews 的多 View 适配器上正确使用 RecycledViewPool?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62950908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com