我正在构建一个配方应用程序(静态数据)。
我创建了一个“食谱”类,在单例对象中,我创建了一个食谱实例列表。
Recipes类包含以下参数:
title: String
ingredients: List<String>
instructions : String
time: Long serves:
Int cost: String
source : String
现在我想为配料创建一个适配器,所以我可以为配料列表创建一个RecyclerView
。
我的问题是将配料列表传递给适配器,而不是配方列表。
我正在尝试不同的事情,但不太确定该怎么做。已经坚持了几天这个问题。
这是食谱类:
class Recipes(val title: String, val ingredients: List<String>, val instructions: String, val time: Long, val serves: Int, val level: String, val source: String) {
private val prepTimeHours: Long = time / 60
private val preTimeMinutes: Long = time.rem(60)
val prepTime: String = "${prepTimeHours.toString()}:${preTimeMinutes.toString()}"
val peopleServed: String = "$serves adults"
}
这是Singleton:
object AllRecipes {
val recipeBook = listOf(
Recipes ("Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
Recipes ("2Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
Recipes ("3Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
Recipes ("4Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
Recipes ("5Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
Recipes ("6Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
Recipes ("7Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
Recipes ("8Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"))
}
这是我目前的适配器:
class IngredientsAdapter(val context: Context, val ingredient: List<Recipes>) : RecyclerView.Adapter<IngredientsAdapter.Holder>() {
inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val singleIngredient = itemView.findViewById<TextView>(R.id.single_ingredient)
fun bindText(textVar: Recipes, context: Context) {
singleIngredient.text = textVar.ingredients[adapterPosition]
}
}
override fun onBindViewHolder(holder: Holder, position: Int) {
holder.bindText(ingredient[position], context)
}
override fun getItemCount(): Int {
return ingredient.count()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.ingredients_layout, parent, false)
return Holder(view)
}
}
任何帮助,将不胜感激!
你可以看看answer here。关键的想法是为每种成分添加RecyclerView
的每个项目的额外视图。实际上,您不必为您的案例实现嵌套的RecyclerView
。只是为每个项目添加更多布局应该没问题,因为成分是有限的。
answer显示了三种不同的方式。如果你的成分数量不是那么大,你可以有多个TextView
s以及你的主要RecyclerView
的每个项目以及View.GONE
的默认可见性。根据您拥有的成分数量,您可以考虑启用某些成分的可见性,而另一成分仍然不可见。
希望有所帮助!