我RecyclerView
在使用5适配器的地方很复杂ViewHolders
。我创建了类似于“联系人列表”的自定义部分。但是在我的recyclerView中,一个区域中的项目可以重置其他区域中的所有内容。但是我不知道该怎么做。
我现在正在做的是擦除整个适配器列表并再次添加项,但这不是有效的方法。里面的项目也Section 1
包含CheckBox,如果选中,则可以清除Section 2
和中的所有数据Section 3
。
OnCheckedChangeListener
CheckBox
从itemList[pos]
状态设置状态;从状态设置状态onBindViewHolder
(在绑定开始时检查部分内部的默认选中项目)。如果选中,则将刷新整个列表并调用notifyDataSetChanged
。这样做是因为不同部分中的所有其他项目都是基于第一部分中的选中项目。
由于导致崩溃concurrent modification exception
。每当这些项目之一再次绑定到onBindViewHolder
选中的更改侦听器内时,就会触发并刷新整个列表。如果连续两次或更多次调用,它将使我的应用程序崩溃。同样,如果用户将上下滚动并且Section1项目开始重新出现和消失,它将在一行中多次调用onBindViewHolder =>多次调用notifyDataSetChanged()=>异常。
AndroidRecyclerView
确实缺乏对部分的本地支持。仅在开始时设置子数组之类的子数组然后调用adapter.updateSection(1)
例如,这真的很酷。
如果我可以RecyclerView
为每个部分单独设置,这也会更好,但是整个RecyclerView必须是可滚动的。加入4RecyclerViews
里面NestedScrollView
会杀回收机制。
用于刷新项目的代码预览:
val selectedVariantFirstSectionJSONObject = getSelectedVariantInJSON(selectedVariantID)
ingredientsItemList.apply {
clear()
add(IngredientHeaderItemNonIconified(getString(R.string.food_ingredients_variant_label)))
addAll(foodVariantsArray)
add(IngredientSectionSeparator())
add(IngredientHeaderItemNonIconified(getString(R.string.food_ingredients_required_ingredients_label)))
add(RequiredIngredient(getVariantRequiredIngredients(selectedVariantJSONObject.getJSONArray(getString(R.string.food_variant_required_ingredients)))))
add(IngredientSectionSeparator())
add(IngredientHeaderItemIconified(getString(R.string.food_ingredients_remove_ingredients_label), TYPE_INGREDIENT_REM))
addAll(removableIngredients)
add(IngredientSectionSeparator())
add(IngredientHeaderItemIconified(getString(R.string.food_ingredients_add_ingredients_label), TYPE_INGREDIENT_OPT))
addAll(optionalIngredients)
}
adapter.notifyDataSetChanged()
便于理解的图片(有点复杂):
RvAdapter.notifyItemRangeChanged(start,count);
如果有的话,您可以简单地为子适配器调用和相同的东西
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句