Код: Выделить всё
class RecommendationRecipeAdapter: ListAdapter(DiffUtilCallback()) {
private val converter = Converters()
// Companion object to hold the view types
companion object {
private const val viewTypeParent = 0
private const val viewTypeChildren = 1
}
override fun getItemViewType(position: Int): Int {
return if (position % 2 == 0) {
viewTypeParent
} else {
viewTypeChildren
}
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return when (viewType) {
viewTypeParent -> ParentViewHolder(inflater.inflate(R.layout.item_date, parent, false))
viewTypeChildren -> ChildViewHolder(inflater.inflate(R.layout.item_food,parent,false))
else -> throw IllegalArgumentException("Please provide a valid viewType")
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val index = position / 2 // Convert position to index in the original dataset
val item = getItem(index)
if (position % 2 == 0) {
if (holder is ParentViewHolder) {
holder.bind(item)
}
} else {
if (holder is ChildViewHolder) {
holder.bind(item)
}
}
}
override fun getItemCount(): Int {
return super.getItemCount() * 2
}
inner class ParentViewHolder(itemView: View) : BaseViewHolder(itemView) {
val dateTextView: TextView = itemView.findViewById(R.id.textViewDate)
override fun bind(item: RecommendationRecipe) {
val date = converter.formatDayDateToString(item.recommendation?.date)
dateTextView.text = date
}
}
inner class ChildViewHolder(itemView: View) : BaseViewHolder(itemView) {
val tvName: TextView = itemView.findViewById(R.id.tv_name)
val tvIngredients: TextView = itemView.findViewById(R.id.tv_ingredients)
val tvCalories: TextView = itemView.findViewById(R.id.tv_calories)
val tvProtein: TextView = itemView.findViewById(R.id.tv_protein)
val tvFat: TextView = itemView.findViewById(R.id.tv_fat)
val tvCarbs: TextView = itemView.findViewById(R.id.tv_carbs)
override fun bind(item: RecommendationRecipe) {
tvName.text = item.recipe?.name
tvIngredients.text = item.recipe?.ingredients
tvCalories.text = item.recipe?.calories.toString()
tvProtein.text = item.recipe?.protein.toString()
tvFat.text = item.recipe?.fat.toString()
tvCarbs.text = item.recipe?.carbs.toString()
}
}
}
abstract class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
abstract fun bind(item: RecommendationRecipe)
}
internal class DiffUtilCallback : DiffUtil.ItemCallback() {
override fun areItemsTheSame(
oldItem: RecommendationRecipe,
newItem: RecommendationRecipe
): Boolean {
return oldItem == newItem
}
override fun areContentsTheSame(
oldItem: RecommendationRecipe,
newItem: RecommendationRecipe
): Boolean {
return oldItem == newItem
}
}
Код: Выделить всё
data class RecommendationRecipe(
@Embedded val recommendation: Recommendation?,
@Relation(
parentColumn = "recipe_id",
entityColumn = "recipeId"
)
val recipe: Recipe?
)
введите здесь описание изображения
Между завтраком , Обед и Ужин, отображаются рецепты 2, 3 и 2 соответственно. Переключаться между просмотром завтрака и ужина вполне нормально. Но когда я нажимаю «Обед», чтобы отобразить рецепты, и возвращаюсь к просмотру «Завтрак» или «Ужин», происходит сбой и возникает следующая ошибка:
Код: Выделить всё
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionChildViewHolder{39148ce position=4 id=-1, oldPos=5, pLpos:5 scrap [attachedScrap] tmpDetached no parent} androidx.recyclerview.widget.RecyclerView{905cbf7 VFED..... ......I. 33,786-1047,2780 #7f09033a app:id/weeklyRecommendationRecyclerView}, , layout:androidx.recyclerview.widget.LinearLayoutManager@43d54cdи изменение способа сопоставления данных, но пока ни одно из них не помогло, за исключением добавления
Код: Выделить всё
override fun getItemCount(): Int {
return super.getItemCount() * 2
}
введите здесь описание изображения
Однако как ни странно, это время, когда приложение не вылетает и не выдает ошибку выше, независимо от того, сколько раз я переключался между завтраком, обедом и ужином. Таким образом, ни один из возможных вариантов сейчас невозможен.
Я попробовал удалить
Код: Выделить всё
override fun getItemCount(): Int {
return super.getItemCount() * 2
}
Пытался удалить его, но recyclerView не раздувается, как ожидалось.
Подробнее здесь: https://stackoverflow.com/questions/783 ... iews-prope
Мобильная версия